【问题标题】:php resize & crop image only if exceeding certain pixel sizephp仅在超过特定像素大小时才调整大小并裁剪图像
【发布时间】:2017-04-27 02:49:48
【问题描述】:

我有一个脚本,可以在服务器上缩放图像并保存到自身,这很好,但我喜欢修改它,因此该脚本仅适用于原始图像超过特定像素大小并最终裁剪结果的情况,

所以流程是 1. 如果源图像高于 80 像素或宽于 300 像素,则继续 2. 将源图像按比例缩放到 80 像素高 3. 如果新宽度超过 300 像素,则从左边缘开始将图像裁剪为 300 像素 4. 将图像保存到自身

我用来缩放的php是

<?php
$org_info = getimagesize("test.jpg");
$rsr_org = imagecreatefromjpeg("test.jpg");
$rsr_scl = imagescale($rsr_org, 320, 80,  IMG_BICUBIC_FIXED);
imagejpeg($rsr_scl, "test.jpg");
imagedestroy($rsr_org);
imagedestroy($rsr_scl);
?>

感谢任何帮助,谢谢

【问题讨论】:

    标签: php image resize scale


    【解决方案1】:

    您可以使用 getimagesize (http://php.net/manual/en/function.getimagesize.php) 在 php 中获取图像大小。

    并且 imagecopyresized 以使用新尺寸调整图像大小 (http://php.net/manual/fr/function.imagecopyresized.php)。

    $size = getimagesize($filename);
    // $size[0] is width
    // $size[1] is height
    
    $thumb = imagecreatetruecolor($newwidth, $newheight);
    $source = imagecreatefromjpeg($filename);
    imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-11
      • 1970-01-01
      • 2010-11-03
      • 2023-03-22
      • 2021-10-07
      • 1970-01-01
      相关资源
      最近更新 更多