在 PHP 中裁剪图像

Posted

技术标签:

【中文标题】在 PHP 中裁剪图像【英文标题】:Cropping an image in PHP 【发布时间】:2010-07-26 12:48:12 【问题描述】:

我正在使用此功能进行 PNG 图像裁剪。一切正常,但每当我上传 .png 时,图像背景颜色都会变为黑色。这是代码。

$bgimage_attribs = getimagesize($bgim_file_name);

        if($filetype3=='image/gif') 
            
                $bgim_old = imagecreatefromgif($bgim_file_name); 
               
        else if(($filetype3=='image/pjpeg') || ($filetype3=='image/jpeg'))  
            
                 $bgim_old = imagecreatefromjpeg($bgim_file_name);
            
          else  if(($filetype3=='image/png') || ($filetype3=='image/x-png'))
            
                 $bgim_old = imagecreatefrompng($bgim_file_name);
            

        $bgth_max_width =265; //for Album image
        $bgth_max_height =150;
        $bgratio = ($bgwidth > $bgheight) ? $bgth_max_width/$bgimage_attribs[0] : $bgth_max_height/$bgimage_attribs[1];

        $bgth_width =265;//$image_attribs[0] * $ratio; 
        $bgth_height =150;//$image_attribs[1] * $ratio;

        $bgim_new = imagecreatetruecolor($bgth_width,$bgth_height); 
        imageantialias($bgim_new,true); 
        $bgth_file_name = "partners_logs/250x150/$Attachments3";
        imagecopyresampled($bgim_new,$bgim_old,0,0,0,0,$bgth_width,$bgth_height, $bgimage_attribs[0], $bgimage_attribs[1]);
        if($filetype3=='image/gif') 
            
                imagegif($bgim_new,$bgth_file_name,100);
                //$bgim_old = imagegif($bgim_file_name); 
               
        else if(($filetype3=='image/pjpeg') || ($filetype3=='image/jpeg'))  
            
                 imagejpeg($bgim_new,$bgth_file_name,100);
            
          else  if(($filetype3=='image/png') || ($filetype3=='image/x-png'))
            
                 imagepng($bgim_new,$bgth_file_name,9);
                       `

即使我也这样尝试过

/************************************Resizing the image 80*60****************/
        $path3="partners_logs";
        $filetype3=$_FILES["pimage"]["type"];
        $bgim_file_name = $path3."/".$Attachments2; 
        $bgimage_attribs = getimagesize($bgim_file_name);

        if($filetype3=='image/gif') 
            
                $bgim_old = imagecreatefromgif($bgim_file_name); 
               
        else if(($filetype3=='image/pjpeg') || ($filetype3=='image/jpeg'))  
            
                 $bgim_old = imagecreatefromjpeg($bgim_file_name);
            
          else  if(($filetype3=='image/png') || ($filetype3=='image/x-png'))
            
                 $bgim_old = imagecreatefrompng($bgim_file_name);
                 imageAlphaBlending($bgim_old, true);

imageSaveAlpha($bgim_old, true);

        $bgth_max_width =265; //for Album image
        $bgth_max_height =150;
        $bgratio = ($bgwidth > $bgheight) ? $bgth_max_width/$bgimage_attribs[0] : $bgth_max_height/$bgimage_attribs[1];

        $bgth_width =265;//$image_attribs[0] * $ratio; 
        $bgth_height =150;//$image_attribs[1] * $ratio;

        $bgim_new = imagecreatetruecolor($bgth_width,$bgth_height); 
        imageantialias($bgim_new,true); 
        $bgth_file_name = "partners_logs/250x150/$Attachments2";
        imagecopyresampled($bgim_new,$bgim_old,0,0,0,0,$bgth_width,$bgth_height, $bgimage_attribs[0], $bgimage_attribs[1]);
        if($filetype3=='image/gif') 
            
                imagegif($bgim_new,$bgth_file_name,100);
                //$bgim_old = imagegif($bgim_file_name); 
               
        else if(($filetype3=='image/pjpeg') || ($filetype3=='image/jpeg'))  
            
                 imagejpeg($bgim_new,$bgth_file_name,100);
            
          else  if(($filetype3=='image/png') || ($filetype3=='image/x-png'))
            
                 imagepng($bgim_new,$bgth_file_name,9);
                   //set the background color to your choice, paramters are int values of red,green and blue  

imagecolorallocate($bgim_new,0xFF,0xFF,0xFF); /************结束调整大小为 80*60*******************/

【问题讨论】:

【参考方案1】:

也许这有帮助

别忘了 imagealphablending() 和 如果你正在工作,imagesavealpha() 带有 [半] 透明 png。

<?php
$file = 'semitransparent.png'; // path to png image
$img = imagecreatefrompng($file); // open image
imagealphablending($img, true); // setting alpha blending on
imagesavealpha($img, true); // save alphablending setting (important)

在用户贡献的注释下的 php.net - imagecreatefrompng 找到

未测试

编辑见评论

<?php
$bgim_new = imagecreatetruecolor($bgth_width,$bgth_height);
imageantialias($bgim_new,true);
imageAlphaBlending($bgim_new, true);
imageSaveAlpha($bgim_new, true);
?>

经过几个 pnp.net cmets 的测试和阅读后,我有了一个工作脚本

<?php
$newImageName = "PNG_transparency_copy.png";

$newWidth = 265;
$newHeight = 150;

$orgAttribs = getimagesize("PNG_transparency_demonstration_1.png");

$orginal = imagecreatefrompng("PNG_transparency_demonstration_1.png");
imagesavealpha($orginal, true);

$newPng = imagecreatetruecolor($newWidth,$newHeight);

imagealphablending($newPng, false);
$color = imagecolortransparent($newPng, imagecolorallocatealpha($newPng, 0, 0, 0, 127));
imagefill($newPng, 0, 0, $color);
imagesavealpha($newPng, true);

imagecopyresampled($newPng,$orginal,0,0,0,0,$newWidth,$newHeight, $orgAttribs[0], $orgAttribs[1]);

header("Content-type: image/png");
imagepng($newPng,$newImageName);

PNG取自Wikipedia

【讨论】:

它可以很好地调整大小(缩放到大约 10%),但是你如何裁剪图像以从中获取一个块? 查看 imagecopyresampled 参数。您需要使用这些值。【参考方案2】:

试试这个功能....

<?php
function CropImg($img_dst, $img_src, $dst_width = 300, $dst_height = 180)
$ext = pathinfo($img_src, PATHINFO_EXTENSION);
if($ext != 'jpg' and $ext != 'jpeg')
    $image = imagecreatefrompng($img_src);

else
    $image = imagecreatefromjpeg($img_src);

$filename = $img_dst;

$thumb_width = $dst_width;
$thumb_height = $dst_height;

$width = imagesx($image);
$height = imagesy($image);

$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;

if ( $original_aspect >= $thumb_aspect )

   // If image is wider than thumbnail (in aspect ratio sense)
   $new_height = $thumb_height;
   $new_width = $width / ($height / $thumb_height);

else

   // If the thumbnail is wider than the image
   $new_width = $thumb_width;
   $new_height = $height / ($width / $thumb_width);


$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );

if($ext == 'png')
    imagealphablending($thumb, false);
    $white = imagecolorallocatealpha($thumb, 0, 0, 0, 127);    //FOR WHITE BACKGROUND
    imagefilledrectangle($thumb,0,0,$thumb_width,$thumb_height,$white);
    imagesavealpha($thumb, true);


// Resize and crop
imagecopyresampled($thumb,
                   $image,
                   0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
                   0 - ($new_height - $thumb_height) / 2, // Center the image vertically
                   0, 0,
                   $new_width, $new_height,
                   $width, $height);
if($ext == 'png')
    imagepng($thumb, $filename);
else
    imagejpeg($thumb, $filename, 80);

return true;


CropImg("photo.png", "result.png", 300, 180);
?>

【讨论】:

以上是关于在 PHP 中裁剪图像的主要内容,如果未能解决你的问题,请参考以下文章

在 PHP 中裁剪图像会留下黑色空间

在 PHP 中裁剪图像

如何在 PHP 中裁剪具有尺寸(没有质量损失)的图像?

这个调整图像大小/裁剪图像的逻辑是不是正确(在 php 中,但它是关于逻辑而不是代码)

我需要一些帮助在 PHP (GD) 中裁剪图像

使用 php 从图像中裁剪多边形