2010-12-14 158 views
3

好吧,基本上我希望所有图像都是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); 
} 
+0

请修复您的格式。 – metrobalderas 2010-12-14 19:43:22

+0

我提供了一个工作脚本,请检查解决(见下文) – Teson 2010-12-15 23:18:30

回答

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_w和高度src_h和src_image在位置(从src_x,src_y),并将其放置在 中的dst_image的矩形区域的宽度dst_w和高度dst_h在 位置(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] 
    ); 
} 
+0

你可能会评论额外的黑客你已经利用。此外,给我最麻烦的是imagecopyresampled()函数的所有不同方面...谢谢 – 2010-12-14 21:05:22

+0

查看更新的答案。 “丑陋的黑客”很可能是一个糟糕的选择。事情是我有一个图像处理类,并显示一个作物的例子,我不得不调整一下逻辑。因此,丑陋的黑客:) – 2010-12-14 21:26:44

0

是否使用ImageMagic?如果没有,你应该。 http://php.net/manual/en/book.imagick.php

+0

OP tag gd ...你可以通过一些例子说服他吗? – ajreal 2010-12-14 20:14:34

+0

不是..?这是原则可以达到我想要的.. id喜欢坚持我所知道的..谢谢 – 2010-12-14 21:06:13

+0

我想我不知道你在用什么。我可以举一些例子,其实很简单。但是,使用ImageMagick意味着您需要将它安装在承载应用程序的系统中。这通常是在* nix系统中的情况,而不是在Windows上。 – 2010-12-15 15:03:16

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);