[英]Cropping an image via GD and sticking with the top part
我已經有了這個腳本,並且一直在嘗試更改它,這使我不知所措,而不是裁剪圖像的中心部分,而是從頂部裁剪到發送給函數的$ height,這將是非常有用的:
function img_resizer($src,$quality,$w,$h,$saveas) {
/* v2.5 with auto crop */
$r=1;
$e=strtolower(substr($src,strrpos($src,".")+1,3));
if (($e == "jpg") || ($e == "peg")) {
$OldImage=ImageCreateFromJpeg($src) or $r=0;
} elseif ($e == "gif") {
$OldImage=ImageCreateFromGif($src) or $r=0;
} elseif ($e == "bmp") {
$OldImage=ImageCreateFromwbmp($src) or $r=0;
} elseif ($e == "png") {
$OldImage=ImageCreateFromPng($src) or $r=0;
} else {
_o("Not a Valid Image! (".$e.") -- ".$src);$r=0;
}
if ($r) {
list($width,$height)=getimagesize($src);
// check if ratios match
$_ratio=array($width/$height,$w/$h);
if ($_ratio[0] != $_ratio[1]) { // crop image
// find the right scale to use
$_scale=min((float)($width/$w),(float)($height/$h));
// coords to crop
$cropX=(float)($width-($_scale*$w));
$cropY=(float)($height-($_scale*$h));
// cropped image size
$cropW=(float)($width-$cropX);
$cropH=(float)($height-$cropY);
$crop=ImageCreateTrueColor($cropW,$cropH);
// crop the middle part of the image to fit proportions
ImageCopy(
$crop,
$OldImage,
0,
0,
(int)($cropX/2),
(int)($cropY/2),
$cropW,
$cropH
);
}
// do the thumbnail
$NewThumb=ImageCreateTrueColor($w,$h);
if (isset($crop)) { // been cropped
ImageCopyResampled(
$NewThumb,
$crop,
0,
0,
0,
0,
$w,
$h,
$cropW,
$cropH
);
ImageDestroy($crop);
} else { // ratio match, regular resize
ImageCopyResampled(
$NewThumb,
$OldImage,
0,
0,
0,
0,
$w,
$h,
$width,
$height
);
}
_ckdir($saveas);
ImageJpeg($NewThumb,$saveas);
ImageDestroy($NewThumb);
ImageDestroy($OldImage);
}
return $r;
}
img_resizer("profile.jpg","",114,89,"profile_small.jpg");
更改一行就可以解決問題:
// crop from top of the image to fit proportions
ImageCopy(
$crop,
$OldImage,
0,
0,
(int)($cropX/2),
(int)(0),
$cropW,
$cropH
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.