PHP - 如何将矩形图像转换为方形图像?

时间:2014-03-30 06:17:21

标签: php image image-processing type-conversion

如何在php中将矩形图像更改为方形头像,这样无论上传图像的分辨率如何,它都可以调整为集中的42 x 42像素头像。这是我正在使用的PHP代码。任何人都可以提供建议。

<?php 
//Name you want to save your file as
$save = 'myfile1.jpg';

$file = 'original1.jpg'; 
echo "Creating file: $save"; 
$size = 0.45; 
header('Content-type: image/jpeg') ; 
list($width, $height) = getimagesize($file) ; 
$modwidth = $width * $size; 
$modheight = $height * $size; 
$tn = imagecreatetruecolor($modwidth, $modheight) ; 
$image = imagecreatefromjpeg($file) ; 
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 

// Here we are saving the .jpg, you can make this gif or png if you want
//the file name is set above, and the quality is set to 100%
imagejpeg($tn, $save, 100) ; 
?> 

2 个答案:

答案 0 :(得分:6)

首先,你需要告诉尺寸为$ x和$ y的矩形中心方块在哪里。

// horizontal rectangle
if ($x > $y) {
    $square = $y;              // $square: square side length
    $offsetX = ($x - $y) / 2;  // x offset based on the rectangle
    $offsetY = 0;              // y offset based on the rectangle
}
// vertical rectangle
elseif ($y > $x) {
    $square = $x;
    $offsetX = 0;
    $offsetY = ($y - $x) / 2;
}
// it's already a square
else {
    $square = $x;
    $offsetX = $offsetY = 0;
}

现在我们可以从中构建一个正方形,只需将其调整为42x42即可。这样的事情应该有效:

list($x, $y) = getimagesize($file);

// code snippet from above goes here
// so we get the square side and the offsets

$endSize = 42;
$tn = imagecreatetruecolor($endSize, $endSize);
imagecopyresampled($tn, $image, 0, 0, $offsetX, $offsetY, $endSize, $endSize, $square, $square);

所以,如果我们有一个100x80的矩形图像,代码会发现大方块大小为80,x offset为10,y offset为0.粗略地说,它看起来像这样:

    100
-----------
|         |
|         | 80
|         |
-----------

     |
     V

    80
 ---------               42
 |       |             -----
 |       | 80   --->   |   | 42
 |       |             -----
 ---------

在我们从原始矩形裁剪大方块后,我们只是缩小到最终大小,在你的情况下为42。


刚测试并且工作正常,如果您打算将图像输出到浏览器(与标题结合),请确保删除回显线。

答案 1 :(得分:2)

我不知道是否有人在寻找这个,但我需要一个600 x 600平方的图像。

源可以是任何矩形图像,我需要保留原始图像的比例,并将原始矩形图像置于600 x 600平方白色图像中。

这是

  • src_file:源图像的URL
  • destination_file:将保存目标文件的路径。
  • square_dimensions(像素)。我需要600,但可以是任何价值。
  • jpeg_quality:质量(0,100)。我使用默认的90

    function square_thumbnail_with_proportion($src_file,$destination_file,$square_dimensions,$jpeg_quality=90)
    {
        // Step one: Rezise with proportion the src_file *** I found this in many places.
    
        $src_img=imagecreatefromjpeg($src_file);
    
        $old_x=imageSX($src_img);
        $old_y=imageSY($src_img);
    
        $ratio1=$old_x/$square_dimensions;
        $ratio2=$old_y/$square_dimensions;
    
        if($ratio1>$ratio2)
        {
            $thumb_w=$square_dimensions;
            $thumb_h=$old_y/$ratio1;
        }
        else    
        {
            $thumb_h=$square_dimensions;
            $thumb_w=$old_x/$ratio2;
        }
    
        // we create a new image with the new dimmensions
        $smaller_image_with_proportions=ImageCreateTrueColor($thumb_w,$thumb_h);
    
        // resize the big image to the new created one
        imagecopyresampled($smaller_image_with_proportions,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
    
        // *** End of Step one ***
    
        // Step Two (this is new): "Copy and Paste" the $smaller_image_with_proportions in the center of a white image of the desired square dimensions
    
        // Create image of $square_dimensions x $square_dimensions in white color (white background)
        $final_image = imagecreatetruecolor($square_dimensions, $square_dimensions);
        $bg = imagecolorallocate ( $final_image, 255, 255, 255 );
        imagefilledrectangle($final_image,0,0,$square_dimensions,$square_dimensions,$bg);
    
        // need to center the small image in the squared new white image
        if($thumb_w>$thumb_h)
        {
            // more width than height we have to center height
            $dst_x=0;
            $dst_y=($square_dimensions-$thumb_h)/2;
        }
        elseif($thumb_h>$thumb_w)
        {
            // more height than width we have to center width
            $dst_x=($square_dimensions-$thumb_w)/2;
            $dst_y=0;
    
        }
        else
        {
            $dst_x=0;
            $dst_y=0;
        }
    
        $src_x=0; // we copy the src image complete
        $src_y=0; // we copy the src image complete
    
        $src_w=$thumb_w; // we copy the src image complete
        $src_h=$thumb_h; // we copy the src image complete
    
        $pct=100; // 100% over the white color ... here you can use transparency. 100 is no transparency.
    
        imagecopymerge($final_image,$smaller_image_with_proportions,$dst_x,$dst_y,$src_x,$src_y,$src_w,$src_h,$pct);
    
        imagejpeg($final_image,$destination_file,$jpeg_quality);
    
        // destroy aux images (free memory)
        imagedestroy($src_img); 
        imagedestroy($smaller_image_with_proportions);
        imagedestroy($final_image);
    }