2010-01-05 181 views

回答

23

你可以使用imagecopy裁剪图像的必要部分。命令是这样的:

imagecopy ( 
    resource $dst_im - the image object , 
    resource $src_im - destination image , 
    int $dst_x - x coordinate in the destination image (use 0) , 
    int $dst_y - y coordinate in the destination image (use 0) , 
    int $src_x - x coordinate in the source image you want to crop , 
    int $src_y - y coordinate in the source image you want to crop , 
    int $src_w - crop width , 
    int $src_h - crop height 
) 

代码从PHP.net - 一个80x40像素的图片,从源图像

<?php 
// Create image instances 
$src = imagecreatefromgif('php.gif'); 
$dest = imagecreatetruecolor(80, 40); 

// Copy 
imagecopy($dest, $src, 0, 0, 20, 13, 80, 40); 

// Output and free from memory 
header('Content-Type: image/gif'); 
imagegif($dest); 

imagedestroy($dest); 
imagedestroy($src); 
?> 
+0

感谢您的快速回答! – user244228 2010-01-05 20:55:27

+0

不会这种方法将黑色背景添加到PNG图像?我的意思是透明背景? – bayblade567 2015-06-04 09:41:54

1

裁剪要使用你需要使用的GD方法的组合GD裁剪图像,如果你看看imagecopyresampled方法的PHP文档中的“Example#1”,它会告诉你如何裁剪和输出图像,你只需要添加一些代码来捕获输出并将其写入文件。 。

http://us2.php.net/manual/en/function.imagecopyresampled.php

还有其他选项,包括Image Magick,如果安装在服务器上,可以直接使用PHP的exec方法(或类似)访问或可以安装PHP Imagick扩展,这将产生更高质量的图像,并在我的看法是,更直观,更灵活。

最后,我使用了开源的PHPThumb类库,它有一个非常简单的界面,可以根据服务器上的内容(包括ImageMagick和GD)使用多个选项。

0

我在一些项目中使用这个脚本,它很容易使用: http://shiftingpixel.com/2008/03/03/smart-image-resizer/

该脚本需要PHP 5.1.0(这是因为从2005-11-24 - 时间在此,如果不升级还版本)和GD(这很少从好的Web主机中丢失)。

这里是它的一个例子就是在你的HTML使用:

<img src="/image.php/coffee-bean.jpg?width=200&amp;height=200&amp;image=/wp-content/uploads/2008/03/coffee-bean.jpg" alt="Coffee Bean" /> 
+0

shiftingpixel。COM似乎暂时尝试链接:) – AlexV 2010-01-05 21:02:41

+0

shiftingpixel.com现在回来了:) – AlexV 2010-01-11 14:35:14

3

此功能将裁剪图像保持图像的长宽比:)

function resize_image_crop($image, $width, $height) 
    { 

     $w = @imagesx($image); //current width 

     $h = @imagesy($image); //current height 
     if ((!$w) || (!$h)) { $GLOBALS['errors'][] = 'Image couldn\'t be resized because it wasn\'t a valid image.'; return false; } 
     if (($w == $width) && ($h == $height)) { return $image; } //no resizing needed 
     $ratio = $width/$w;  //try max width first... 
     $new_w = $width; 
     $new_h = $h * $ratio;  
     if ($new_h < $height) { //if that created an image smaller than what we wanted, try the other way 
      $ratio = $height/$h; 
      $new_h = $height; 
      $new_w = $w * $ratio; 
     } 
     $image2 = imagecreatetruecolor ($new_w, $new_h); 
     imagecopyresampled($image2,$image, 0, 0, 0, 0, $new_w, $new_h, $w, $h);  
     if (($new_h != $height) || ($new_w != $width)) { //check to see if cropping needs to happen 
      $image3 = imagecreatetruecolor ($width, $height); 
      if ($new_h > $height) { //crop vertically 
       $extra = $new_h - $height; 
       $x = 0; //source x 
       $y = round($extra/2); //source y 
       imagecopyresampled($image3,$image2, 0, 0, $x, $y, $width, $height, $width, $height); 
      } else { 
       $extra = $new_w - $width; 
       $x = round($extra/2); //source x 
       $y = 0; //source y 
       imagecopyresampled($image3,$image2, 0, 0, $x, $y, $width, $height, $width, $height); 
      } 
      imagedestroy($image2); 
      return $image3; 
     } else { 
      return $image2; 
     } 
    } 
+0

不应该需要做多个imagecopyresampled调用,它可以在一次调用中完成,如果偏移量计算为$ width和$高度。此外,总是强制调整大小,以确保图像保存为新格式,并删除额外的垃圾和标题并强制为特定质量。 – Exit 2016-12-02 13:29:03

-1

您可以使用下面的方法即可裁剪图片,

/*parameters are 
    $image =source image name 
    $width = target width 
    $height = height of image 
    $scale = scale of image*/ 
    function resizeImage($image,$width,$height,$scale) { 
     //generate new image height and width of source image 
     $newImageWidth = ceil($width * $scale); 
     $newImageHeight = ceil($height * $scale); 
     //Create a new true color image 
     $newImage = imagecreatetruecolor($newImageWidth,$newImageHeight); 
     //Create a new image from file 
     $source = imagecreatefromjpeg($image); 
     //Copy and resize part of an image with resampling 
     imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height); 
     //Output image to file 
     imagejpeg($newImage,$image,90); 
     //set rights on image file 
     chmod($image, 0777); 
     //return crop image 
     return $image; 
    } 
+0

如果您在代码中添加注释以解释关键部件的功能,可能对用户有所帮助。这将帮助他们更好地理解和应用您的代码 – shrmn 2015-11-27 06:50:51

+0

好的,我已经添加了 – 2015-11-27 07:31:25

0

我刚创建了这个功能,它适用于我的需求,创建一个居中和裁剪缩略图图像。它是流线型的,并不需要像webGutam的答案中所示的多个图像拷贝调用。

提供图像路径,最终宽度和高度,以及可选的图像质量。我为此创建了缩略图,因此所有图像都保存为JPG,如果需要,可以对其进行编辑以适应其他图像类型。这里的要点是使用imagecopyresampled来产生缩略图的数学和方法。使用相同的名称和图像大小保存图像。

function resize_crop_image($image_path, $end_width, $end_height, $quality = '') { 
if ($end_width < 1) $end_width = 100; 
if ($end_height < 1) $end_height = 100; 
if ($quality < 1 || $quality > 100) $quality = 60; 

$image = false; 
$dot = strrpos($image_path,'.'); 
$file = substr($image_path,0,$dot).'-'.$end_width.'x'.$end_height.'.jpg'; 
$ext = substr($image_path,$dot+1); 

if ($ext == 'jpg' || $ext == 'jpeg') $image = @imagecreatefromjpeg($image_path); 
elseif($ext == 'gif') $image = @imagecreatefromgif($image_path); 
elseif($ext == 'png') $image = @imagecreatefrompng($image_path); 

if ($image) { 
    $width = imagesx($image); 
    $height = imagesy($image); 
    $scale = max($end_width/$width, $end_height/$height); 
    $new_width = floor($scale*$width); 
    $new_height = floor($scale*$height); 
    $x = ($new_width != $end_width ? ($width - $end_width)/2 : 0); 
    $y = ($new_height != $end_height ? ($height - $end_height)/2 : 0); 
    $new_image = @imagecreatetruecolor($new_width, $new_height); 

    imagecopyresampled($new_image,$image,0,0,$x,$y,$new_width,$new_height,$width - $x,$height - $y); 
    imagedestroy($image); 
    imagejpeg($new_image,$file,$quality); 
    imagedestroy($new_image); 

    return $file; 
} 
return false; 
}