PHP调整上传图片
我得到了一个表单,用户正在插入一些数据,并上传图像。
为了处理图像,我得到了以下代码:
define ("MAX_SIZE","10000"); $errors=0; $image =$_FILES["fileField"]["name"]; $uploadedfile = $_FILES['fileField']['tmp_name']; if($image){ $filename = stripslashes($_FILES['fileField']['name']); $extension = strtolower(getExtension($filename)); if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")){ echo ' Unknown Image extension '; $errors=1; } else{ $newname = "$product_cn."."$extension"; $size=filesize($_FILES['fileField']['tmp_name']); if ($size > MAX_SIZE*1024){ echo "You have exceeded the size limit"; $errors=1; } if($extension=="jpg" || $extension=="jpeg" ){ $uploadedfile = $_FILES['file']['tmp_name']; $src = imagecreatefromjpeg($uploadedfile); }else if($extension=="png"){ $uploadedfile = $_FILES['file']['tmp_name']; $src = imagecreatefrompng($uploadedfile); }else{ $src = imagecreatefromgif($uploadedfile); } list($width,$height)=getimagesize($uploadedfile); $newwidth=60; $newheight=($height/$width)*$newwidth; $tmp=imagecreatetruecolor($newwidth,$newheight); $newwidth1=25; $newheight1=($height/$width)*$newwidth1; $tmp1=imagecreatetruecolor($newwidth1,$newheight1); imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight, $width,$height); imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1, $width,$height); $filename = "../products_images/".$newname; $filename1 = "../products_images/thumbs/".$newname; imagejpeg($tmp,$filename,100); //file name also indicates the folder where to save it to imagejpeg($tmp1,$filename1,100); imagedestroy($src); imagedestroy($tmp); imagedestroy($tmp1); } }
getExtension函数:
function getExtension($str) { $i = strrpos($str,"."); if (!$i) { return ""; } $l = strlen($str) - $i; $ext = substr($str,$i+1,$l); return $ext; }
我在代码中写了一些符号,因为我不太熟悉这些函数。
由于某种原因,它不起作用。
当我要去的文件夹“product_images”或“product_images /拇指”我找不到任何图片上传。
任何想法我的代码有什么问题? 应该有60px宽度图像和25px宽度图像。
注意:
variables,你不知道他们被宣布的地方,如$product_cn
之前声明的代码块工作正常(testing它)之前声明。 如果你仍然想看一看,请随时索要密码。
提前致谢!
您可以使用此库在上传时操作图像。 http://www.verot.net/php_class_upload.htm
这是另一个不错的简单的解决scheme:
$maxDim = 800; list($width, $height, $type, $attr) = getimagesize( $_FILES['myFile']['tmp_name'] ); if ( $width > $maxDim || $height > $maxDim ) { $target_filename = $_FILES['myFile']['tmp_name']; $fn = $_FILES['myFile']['tmp_name']; $size = getimagesize( $fn ); $ratio = $size[0]/$size[1]; // width/height if( $ratio > 1) { $width = $maxDim; $height = $maxDim/$ratio; } else { $width = $maxDim*$ratio; $height = $maxDim; } $src = imagecreatefromstring( file_get_contents( $fn ) ); $dst = imagecreatetruecolor( $width, $height ); imagecopyresampled( $dst, $src, 0, 0, 0, 0, $width, $height, $size[0], $size[1] ); imagedestroy( $src ); imagepng( $dst, $target_filename ); // adjust format as needed imagedestroy( $dst ); }
参考:
PHP按照最大宽度或者重量的比例调整图片大小
编辑:清理/简化了一下代码(谢谢@ jan-mirus的评论。)
$maxDim = 800; $file_name = $_FILES['myFile']['tmp_name']; list($width, $height, $type, $attr) = getimagesize( $file_name ); if ( $width > $maxDim || $height > $maxDim ) { $target_filename = $file_name; $ratio = $width/$height; if( $ratio > 1) { $new_width = $maxDim; $new_height = $maxDim/$ratio; } else { $new_width = $maxDim*$ratio; $new_height = $maxDim; } $src = imagecreatefromstring( file_get_contents( $file_name ) ); $dst = imagecreatetruecolor( $new_width, $new_height ); imagecopyresampled( $dst, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height ); imagedestroy( $src ); imagepng( $dst, $target_filename ); // adjust format as needed imagedestroy( $dst ); }
//这是我的例子,我曾经自动调整每个插入的照片的大小为100×50像素,图像格式为JPEG,希望这也有帮助
if($result){ $maxDimW = 100; $maxDimH = 50; list($width, $height, $type, $attr) = getimagesize( $_FILES['photo']['tmp_name'] ); if ( $width > $maxDimW || $height > $maxDimH ) { $target_filename = $_FILES['photo']['tmp_name']; $fn = $_FILES['photo']['tmp_name']; $size = getimagesize( $fn ); $ratio = $size[0]/$size[1]; // width/height if( $ratio > 1) { $width = $maxDimW; $height = $maxDimH/$ratio; } else { $width = $maxDimW*$ratio; $height = $maxDimH; } $src = imagecreatefromstring(file_get_contents($fn)); $dst = imagecreatetruecolor( $width, $height ); imagecopyresampled($dst, $src, 0, 0, 0, 0, $width, $height, $size[0], $size[1] ); imagejpeg($dst, $target_filename); // adjust format as needed } move_uploaded_file($_FILES['pdf']['tmp_name'],"pdf/".$_FILES['pdf']['name']);
下载库文件Zebra_Image.php belo链接
https://drive.google.com/file/d/0Bx-7K3oajNTRV1I2UzYySGZFd3M/view
resizeimage.php
<?php require 'Zebra_Image.php'; // create a new instance of the class $resize_image = new Zebra_Image(); // indicate a source image $resize_image->source_path = $target_file1; $ext = $photo; // indicate a target image $resize_image->target_path = 'images/thumbnil/' . $ext; // resize // and if there is an error, show the error message if (!$resize_image->resize(200, 200, ZEBRA_IMAGE_NOT_BOXED, -1)); // from this moment on, work on the resized image $resize_image->source_path = 'images/thumbnil/' . $ext; ?>
你也可以使用Imagine库。 它使用GD和Imagick。
如果你想使用Imagick(包含在大多数PHP发行版中),它就像…
$image = new Imagick(); $image_filehandle = fopen('some/file.jpg', 'a+'); $image->readImageFile($image_filehandle); $image->scaleImage(100,200,FALSE); $image_icon_filehandle = fopen('some/file-icon.jpg', 'a+'); $image->writeImageFile($image_icon_filehandle);
您可能需要基于原始图像更dynamic地计算宽度和高度。 你可以使用上面的例子获取图像的当前宽度和高度,使用$ image-> getImageHeight(); 和$ image-> getImageWidth();.