好吧,基本上我希望所有图像都是170x170px正方形。 因此,如果图像不是一个正方形,我希望它被调整大小,然后在中间裁剪。调整大小然后裁剪PHP
我花了很多时间玩这个,我越来越无处..我已经得到它来裁剪更大的图像等部分,但我特别需要图像的大小,然后裁剪..
任何帮助将不胜感激。
// get image size of img
$x = @getimagesize($img);
// image width
$sw = $x[0];
// image height
$sh = $x[1];
if($sw > $sh) // Horizontal Rectangle?
{
$newwidth = ($sw/$sh)*170;
$newheight=170;
$x_pos = ($sw - $sh)/2;
$x_pos = ceil($x_pos);
$y_pos=0;
}
else if($sh > $sw) // Vertical Rectangle?
{
$newheight = ($sh/$sw)*170;
$newwidth=170;
$y_pos = ($sh - $sw)/2;
$y_pos = ceil($y_pos);
$x_pos=0;
}
else //Already Square
{
$newheight=170;
$newwidth=170;
}
$im = @ImageCreateFromJPEG ($img) or // Read JPEG Image
$im = @ImageCreateFromPNG ($img) or // or PNG Image
$im = @ImageCreateFromGIF ($img) or // or GIF Image
$im = false; // If image is not JPEG, PNG, or GIF
if (!$im) {
// We get errors from PHP's ImageCreate functions...
// So let's echo back the contents of the actual image.
readfile ($img);
} else {
// Create the resized image destination
$thumb = @ImageCreateTrueColor (170, 170);
// Copy from image source, resize it, and paste to image destination
imagecopyresampled($thumb, $im, 0, 0, 180, $y_pos, 170, 170, $newwidth,
$newheight);
}
请修复您的格式。 – metrobalderas 2010-12-14 19:43:22
我提供了一个工作脚本,请检查解决(见下文) – Teson 2010-12-15 23:18:30