用PHP上传图片并修改尺寸
对于开发一个WEB2.0的网站,对于用户上传图片的功能是少不了的,上传后的图片需要自动调整尺寸,以生成缩略图,无论从页面载入速度上,服务器负重上来看,调整尺寸都是必不可少的。
贴上一段用PHP上传图片并修改尺寸的代码,初学者可以看看。也可以自己修改修改,写成类。
原文地址:Image Uploading and Resizing with PHP
一、HTML代码部分
二、上传图片并保存至服务器的代码 (upload.php)
// This is the temporary file created by PHP $uploadedfile = $_FILES['uploadfile']['tmp_name']; // Create an Image from it so we can do the resize $src = imagecreatefromjpeg($uploadedfile); // Capture the original size of the uploaded image list($width,$height)=getimagesize($uploadedfile); // For our purposes, I have resized the image to be // 600 pixels wide, and maintain the original aspect // ratio. This prevents the image from being "stretched" // or "squashed". If you prefer some max width other than // 600, simply change the $newwidth variable $newwidth=600; $newheight=($height/$width)*$newwidth; $tmp=imagecreatetruecolor($newwidth,$newheight); // this line actually does the image resizing, copying from the original // image into the $tmp image imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); // now write the resized image to disk. I have assumed that you want the // resized, uploaded image file to reside in the ./images subdirectory. $filename = "images/". $_FILES['uploadfile']['name']; imagejpeg($tmp,$filename,100); imagedestroy($src); imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request // has completed.
最后贴上一个网上找到的类 点击打开