PHP GD图像裁剪函数
我使用这段代码制作上传图像的缩略图PHP GD图像裁剪函数,php,file-upload,gd,crop,Php,File Upload,Gd,Crop,我使用这段代码制作上传图像的缩略图 $file = randString().'.jpg'; $uniPath = 'i/u'.$login; $completePath = $uniPath.'/a/'.$file; $thumbPath = $uniPath.'/b/'.$file; if(copy($_FILES['filename']['tmp_name'], $completePath)){ function convertPic($w_dst, $h_
$file = randString().'.jpg';
$uniPath = 'i/u'.$login;
$completePath = $uniPath.'/a/'.$file;
$thumbPath = $uniPath.'/b/'.$file;
if(copy($_FILES['filename']['tmp_name'], $completePath)){
function convertPic($w_dst, $h_dst, $n_img){
$wxh = $w_dst.'x'.$h_dst;
switch($wxh){
case '150x150': $letter = 'b'; break;
case '50x50': $letter = 'c'; break;
default: $letter = 'z';
}
$dbPath = '/i/u1/'.$letter.'/'.$n_img;
$new_img = $_SERVER['DOCUMENT_ROOT'].$dbPath;
$file_src = "img.jpg"; // temporary safe image storage
$img_src = $file_src;
unlink($file_src);
move_uploaded_file($_FILES['filename']['tmp_name'], $file_src);
list($w_src, $h_src, $type) = getimagesize($file_src); // create new dimensions, keeping aspect ratio
$ratio = $w_src/$h_src;
$h_ratio = ($h_dst / $h_src);
$w_ratio = ($w_dst / $w_src);
if($w_src > $h_src){ //landscape
$w_crop = round($w_src * $h_ratio);
$h_crop = $h_dst;
$src_x = ceil(($w_src - $h_src)/2);
$src_y = 0;
}
elseif($w_src < $h_src){ // portrait
$h_crop = round($h_src * $w_ratio);
$w_crop = $w_dst;
$src_y = ceil(($h_src - $w_src)/2);
$src_x = 0;
}
else { //square
$w_crop = $w_dst;
$h_crop = $h_dst;
$src_x = 0;
$src_y = 0;
}
switch ($type)
{case 1: // gif -> jpg
$img_src = imagecreatefromgif($file_src);
break;
case 2: // jpeg -> jpg
$img_src = imagecreatefromjpeg($file_src);
break;
case 3: // png -> jpg
$img_src = imagecreatefrompng($file_src);
break;
}
$img_dst = imagecreatetruecolor($w_dst, $h_dst); // resample
imagecolorallocate($img_dst, 255, 255, 255) or die("fail imagecolorallocate");
imagecopyresampled($img_dst, $img_src, 0, 0, $src_x, $src_y, $w_crop, $h_crop, $w_src, $h_src) or die("imagecopyresampled($img_dst, $img_src, 0, 0, $src_x, $src_y, $w_crop, $h_crop, $w_src, $h_src)");
imagejpeg($img_dst, $new_img); // save new image
unlink($file_src); // clean up image storage
imagedestroy($img_src);
imagedestroy($img_dst);
return $db_path;
}
convertPic(150, 150, $file);
convertPic(250, 250, $file);
$file=randString().jpg';
$uniPath='i/u'。$login;
$completePath=$uniPath.'/a/'.$file;
$thumbPath=$uniPath.'/b/'.$file;
如果(复制($_文件['filename']['tmp_名称],$completePath)){
函数convertPic($w_dst、$h_dst、$n_img){
$wxh=$w_dst'x'.$h_dst;
交换机($wxh){
案例'150x150':$letter='b';中断;
案例'50x50':$letter='c';break;
默认值:$letter='z';
}
$dbPath='/i/u1/'.$letter./'.$n_img;
$new\u img=$\u SERVER['DOCUMENT\u ROOT'].$dbPath;
$file_src=“img.jpg”;//临时安全映像存储
$img\u src=$file\u src;
取消链接($file\u src);
移动上传的文件($文件['filename']['tmp\u name'],$file\u src);
list($w_src,$h_src,$type)=getimagesize($file_src);//创建新维度,保持纵横比
$ratio=$w_src/$h_src;
$h_比率=($h_dst/$h_src);
$w_比率=($w_dst/$w_src);
如果($w_src>$h_src){//
$w_裁剪=圆形($w_src*$h_比率);
$h_作物=$h_dst;
$src_x=ceil($w_src-$h_src)/2);
$src_y=0;
}
elseif($w_src<$h_src){//
$h_裁剪=圆形($h_src*$w_比率);
$w_作物=$w_dst;
$src_y=ceil($h_src-$w_src)/2);
$src_x=0;
}
else{//square
$w_作物=$w_dst;
$h_作物=$h_dst;
$src_x=0;
$src_y=0;
}
交换机($类型)
{案例1://gif->jpg
$img_src=imagecreatefromformgif($file_src);
打破
案例2://jpeg->jpg
$img_src=imagecreatefromjpeg($file_src);
打破
案例3://png->jpg
$img_src=imagecreatefrompng($file_src);
打破
}
$img_dst=imagecreatetruecolor($w_dst,$h_dst);//重新采样
imagecolorallocate($img_dst,255,255)或die(“失败imagecolorallocate”);
imagecopyresampled($img_dst,$img_src,0,0,$src_x,$src_y,$w_crop,$w_src,$h_src)或die(“imagecopyresampled($img_dst,$img_src,0,0,$src_x,$src_y,$w_crop,$h_crop,$w_src,$w_src)”));
imagejpeg($img_dst,$new_img);//保存新图像
取消链接($file_src);//清理图像存储
图像销毁($img\U src);
图像销毁($imgdst);
返回$db_路径;
}
convertPic(150150美元文件);
convertPic(250250美元文件);
但是由于某种原因,convertPic函数如果像上面的例子那样调用两次就不起作用了。如果一次调用它,一切正常。如果imagecopyresampled失败并输出,我会发出警报
imagecopyresampled(资源id#17,
img.jpg,0,0,0,0250,)
我认为问题在于临时图像存储,但不确定。请帮助。似乎您正在处理上载的图像。作为处理功能的一部分,您可以将上载的文件从其临时目录中移动,然后对其进行一些处理 当您第二次再次调用该函数时,上载的文件不再位于临时目录中,事情就会变得一团糟
function convertPic(...) {
....
move_uploaded_file($_FILES['filename']['tmp_name'], $file_src);
....
unlink($file_src);
....
}
基本上,对convertPic的第一次调用会先处理,然后删除“源”,这意味着随后的第二次调用无法再使用它。我想问题在于取消链接($file\u src) 首先,调用
convertPic()
函数,它工作正常,因为img.jpg仍在这里,然后使用unlink()
删除图像,并尝试再次调用实际需要该文件才能正常工作的相同例程
同样,您不能移动同一个文件两次,因此您必须将其移到函数外,根据需要执行多次操作,然后取消图像链接。取消链接应在双重调用convertPic()
之后,而move\u uploaded\u file()
应在函数convertPic()
之前
嗯,这是我第一眼看到的