好的,基本上我希望所有图像都是170x170px的正方形。 因此,如果图像不是正方形,我希望它被调整大小,然后在中间裁剪..
我已经花了很多时间玩这个而且我无处可去..我已经得到它来裁剪更大图像的一部分等,但我特别需要调整图像大小,然后裁剪..
非常感谢任何帮助。
// 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);
}
答案 0 :(得分:4)
<?
$img = 'leaf.jpg';
// get image size of img
$x = @getimagesize($img);
// image dimensions
$sw = $x[0];
$sh = $x[1];
//dest size
$dSize = 170;
//find smallerst part and get needed scale and offset
$yOff = 0;
$xOff = 0;
if($sw < $sh) {
$scale = $dSize / $sw;
$yOff = $sh/2 - $dSize/$scale/2;
} else {
$scale = $dSize / $sh;
$xOff = $sw/2 - $dSize/$scale/2;
}
$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 ($dSize,$dSize);
// Copy from image source, resize it, and paste to image destination
imagecopyresampled($thumb, $im,
0, 0,
$xOff,$yOff,
$dSize, $dSize,
$dSize / $scale ,$dSize / $scale);
}
header('content-type:image/jpeg');
imagejpeg($thumb);
//imagejpeg($im);
答案 1 :(得分:2)
需要一些工作,但它应该足以让你开始。
function crop($filename, $width, $height)
{
// image resource, assuming it's PNG
$resource = imagecreatefrompng($filename);
// resource dimensions
$size = array(
0 => imagesx($resource),
1 => imagesy($resource),
);
// sides
$longer = (int)($size[0]/$width > $size[1]/$height);
$shorter = (int)(!$longer);
// ugly hack to avoid condition for imagecopyresampled()
$src = array(
$longer => 0,
$shorter => ($size[$shorter]-$size[$longer])/2,
);
// new image resource
$new = imagecreatetruecolor($width, $height);
// do the magic
imagecopyresampled($new, $resource,
0, 0,
$src[0], $src[1],
$width, $height,
$size[$longer], $size[$longer]
);
// save it or something else :)
}
编辑:尝试解释上面的“丑陋黑客”。
有问题的两个参数是$src_x
和$src_y
,取自manual:
imagecopyresampled()将从src_image中取一个矩形区域 宽度src_w和高度src_h在位置(src_x,src_y)并将其放入 dst_image的矩形区域,宽度为dst_w,高度为dst_h at position(dst_x,dst_y)。
如果$filename
的宽度较长,则src_x
必须为0
,如果高度较长,则src_y
必须为0
。翻译成代码,它看起来像这样:
$src = ($size[$shorter]-$size[$longer])/2;
if ( $longer === 1 )
{
imagecopyresampled($new, $resource,
0, 0,
$src, 0,
$width, $height,
$size[$longer], $size[$longer]
);
}
else
{
imagecopyresampled($new, $resource,
0, 0,
0, $src,
$width, $height,
$size[$longer], $size[$longer]
);
}
答案 2 :(得分:0)
您使用的是ImageMagick吗?如果没有,你应该。 http://php.net/manual/en/book.imagick.php
答案 3 :(得分:0)
你可以尝试一下,我还没有,但看起来很有希望。 http://phpthumb.sourceforge.net/