Php 将图片调整大小/裁剪/填充为固定大小

Php 将图片调整大小/裁剪/填充为固定大小,php,resize,crop,Php,Resize,Crop,我需要将图片调整为固定大小。但它必须保持宽度和高度之间的因素 假设我想将图片的大小从238(宽)x182(高)调整到210/150 我现在做的是: Original width / target width = 1.333333 Original Height / target Height = 1.213333 现在我取最小的因子 现在我总是有正确的宽度,因为238/1.333333=210。 但高度仍然是160 如何在不破坏图片的情况下将高度降低到160 我需要修剪吗?如果是这样的话,怎么

我需要将图片调整为固定大小。但它必须保持宽度和高度之间的因素

假设我想将图片的大小从
238(宽)x182(高)
调整到
210/150

我现在做的是:

Original width / target width = 1.333333
Original Height / target Height = 1.213333
现在我取最小的因子

现在我总是有正确的宽度,因为
238/1.333333=210
。 但高度仍然是
160

如何在不破坏图片的情况下将高度降低到
160

我需要修剪吗?如果是这样的话,怎么办?

你有什么建议吗?如果是这样的话,您可以加载图像并执行以下操作


在那里,您可以跳过任何一个参数(高度或宽度),它将正确调整大小。

也许可以看看(它与GD和ImageMagick一起工作)

您必须从顶部和底部裁剪5像素才能达到目标大小,但这可能会破坏图片


实际上,您应该有一个目标宽度或高度,然后按相同的比例调整其他维度。

这不会裁剪图片,而是在必要时在新图像周围留出空间,我认为这是创建缩略图时更好的方法(比裁剪)

$w = 210;
$h = 150;

$orig_w = imagesx($original);
$orig_h = imagesy($original);

$w_ratio = $orig_w / $w;
$h_ratio = $orig_h / $h;

$ratio = $w_ratio > $h_ratio ? $w_ratio : $h_ratio;

$dst_w = $orig_w / $ratio;
$dst_h = $orig_h / $ratio;
$dst_x = ($w - $dst_w) / 2;
$dst_y = ($h - $dst_h) / 2;

$thumbnail = imagecreatetruecolor($w, $h);

imagecopyresampled($thumbnail, $original, $dst_x, $dst_y,
                   0, 0, $dst_w, $dst_h, $orig_w, $orig_h);

我宁愿调整大小,使图像包含在你的极限之内,然后填空部分。因此,在上面的示例中,您可以调整大小,使高度合适,然后用背景色填充左右两侧(我认为每端7个像素)。

从PHP来源的网页中调整图像的大小可能会有问题。较大的映像(磁盘上接近2+MB)可能非常大,需要超过32MB的内存才能处理

出于这个原因,我倾向于使用基于CLI的脚本(可用内存高达128MB)或标准命令行(也可以使用它所需的内存)来执行此操作

# where to put the original file/image. It gets resized back 
# it was originally found (current directory)
SAFE=/home/website/PHOTOS/originals
# no more than 640x640 when finished, and always proportional
MAXSIZE=640
# the larger image is in /home/website/PHOTOS/, moved to .../originals
# and the resized image back to the parent dir.
cd $SAFE/.. && mv "$1" "$SAFE/$1" && \
   convert "$SAFE/$1" -resize $MAXSIZE\x$MAXSIZE\> "$1"

“convert”是ImageMagick命令行工具的一部分。

这些是缩略图吗?如果是的话,种植也不是什么大问题。我们总是这样做。我甚至不羞于将任意比例裁剪成残缺的二次缩略图,完全弄乱图像(是的,我就是那个硬核),如果它看起来不错的话。这是设计师对一个技术问题的回答,但仍然是。不要害怕庄稼

我觉得有点混乱。。 如果只想调整其大小,保持原始比率,则正确的操作是:

$ratio = $originalWidth / $originalHeight;
if(//you start from the width, and want to find the height){
 $newWidth = $x;
 $newHeight = $x / $ratio;
}else if(//you start from the height, and want to find the width){
 $newHeight = $x;
 $newWidth = $x * $ratio;
}
否则,如果无法更改前缀newWidth和newHeight,并且拇指比率与原始比率不同,则唯一的方法是裁剪拇指或向拇指添加边框

如果你继续走捷径,这个函数可以帮助你(我几年前在5分钟内写的,可能需要一些改进..它只适用于jpg,例如;):

function thumb\u cut($nomeimage、$source\u path、$destination\u path、$new\u width、$new\u height){
列表($width、$height、$type、$attr)=getimagesize($source_path.$nomeimage);
如果($type==2){
如果($width>$new\u width){
$new_width=$width;
$new_height=$height;
}
$compression=100;
$destimg=imagecreatetruecolor($new_width,$new_height)或die(“创建图像的问题”);
$srcimg=ImageCreateFromJPEG($source_path.$nomeimage)或die(“打开图像时出现问题”);
$w=ImageSX($srcimg);
$h=ImageSY($srcimg);
$ro=$new\u width/$new\u height;
$ri=$w/$h;

如果($ro这个解决方案与Can Berk Güder的基本相同,但是在花了一些时间撰写和评论之后,我想发布

此函数创建一个缩略图,其大小与您给定的大小完全相同。 图像的大小将调整为最适合缩略图的大小。如果它在两个方向上都不完全适合,则它将位于thumnail的中心。大量的评论解释了这一情况

function thumbnail_box($img, $box_w, $box_h) {
    //create the image, of the required size
    $new = imagecreatetruecolor($box_w, $box_h);
    if($new === false) {
        //creation failed -- probably not enough memory
        return null;
    }


    //Fill the image with a light grey color
    //(this will be visible in the padding around the image,
    //if the aspect ratios of the image and the thumbnail do not match)
    //Replace this with any color you want, or comment it out for black.
    //I used grey for testing =)
    $fill = imagecolorallocate($new, 200, 200, 205);
    imagefill($new, 0, 0, $fill);

    //compute resize ratio
    $hratio = $box_h / imagesy($img);
    $wratio = $box_w / imagesx($img);
    $ratio = min($hratio, $wratio);

    //if the source is smaller than the thumbnail size, 
    //don't resize -- add a margin instead
    //(that is, dont magnify images)
    if($ratio > 1.0)
        $ratio = 1.0;

    //compute sizes
    $sy = floor(imagesy($img) * $ratio);
    $sx = floor(imagesx($img) * $ratio);

    //compute margins
    //Using these margins centers the image in the thumbnail.
    //If you always want the image to the top left, 
    //set both of these to 0
    $m_y = floor(($box_h - $sy) / 2);
    $m_x = floor(($box_w - $sx) / 2);

    //Copy the image data, and resample
    //
    //If you want a fast and ugly thumbnail,
    //replace imagecopyresampled with imagecopyresized
    if(!imagecopyresampled($new, $img,
        $m_x, $m_y, //dest x, y (margins)
        0, 0, //src x, y (0,0 means top left)
        $sx, $sy,//dest w, h (resample to this size (computed above)
        imagesx($img), imagesy($img)) //src w, h (the full size of the original)
    ) {
        //copy failed
        imagedestroy($new);
        return null;
    }
    //copy successful
    return $new;
}
用法示例:

$i = imagecreatefromjpeg("img.jpg");
$thumb = thumbnail_box($i, 210, 150);
imagedestroy($i);

if(is_null($thumb)) {
    /* image creation or copying failed */
    header('HTTP/1.1 500 Internal Server Error');
    exit();
}
header('Content-Type: image/jpeg');
imagejpeg($thumb);

只是一个从大图像快速生成高质量缩略图的提示:()

如果在两个阶段生成缩略图:

  • 使用快速调整大小,从原始图像到最终尺寸两倍的中间图像
  • 使用高质量重采样从中间图像到最终缩略图
  • 然后这可以快得多;步骤1中的调整大小质量相对较差,但有足够的额外分辨率,使步骤2中的质量相当好,中间图像足够小,高质量重采样(在2:1调整大小时效果很好)进行得非常快。

    该技术是:

  • 调整图像大小,使一个尺寸匹配,而另一个尺寸超过所需尺寸
  • 从调整大小的图像中心取出所需大小的图像
  • 最后,如果您对如何调整大小感到困惑,请记住,如果源图像和目标图像的比例相同,则此关系成立:

    SourceWidth/SourceHeight=DestinationWidth/DestinationHeight
    
    如果你知道三个参数,你可以很容易地计算出第四个参数

    我为此写了一篇文章:

    我更喜欢使用gd,因为这是我在所有事情上都使用的。不知道我是否有ImageMagick这里有一个imagick php的例子:是的,但是高度或宽度可能与其要求的值不一致。如果你不能完全控制图像的来源,使用特定的图像大小不是一个好主意。你只能调整大小以适应。你测试过这个吗?因为在我的例子中,高度仍然是160像素,而不是150像素,因为它应该是150像素,而宽度是196像素。所以左右两边都有7像素的边距。高度196?应该是150!实际上这个脚本不会调整原始图像的大小。这也可能吗?这个脚本会复制原始图像。什么时候重新采样图像时,您必须制作一个副本。我的示例use会销毁原始图像(imagedestroy($i))。如果要覆盖图像文件,则只需将$thumb写入示例中的文件“img.jpg”。好的,谢谢。如果我想这样做,该怎么办
    $i = imagecreatefromjpeg("img.jpg");
    $thumb = thumbnail_box($i, 210, 150);
    imagedestroy($i);
    
    if(is_null($thumb)) {
        /* image creation or copying failed */
        header('HTTP/1.1 500 Internal Server Error');
        exit();
    }
    header('Content-Type: image/jpeg');
    imagejpeg($thumb);