【问题标题】:Resizing Image max-width and height调整图像最大宽度和高度
【发布时间】:2014-04-04 21:25:12
【问题描述】:

我一直在互联网上寻找解决方案。我正在使用这个 SimpleImage.php 类来调整我在这里找到的图像 (http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php)。到目前为止,一切正常。如果我尝试将图像的大小重新调整为特定的高度或宽度,它正在完成它的工作。但是,这仅在我尝试一次调整图像大小时才有效。假设我的图像的原始尺寸是 1400x1100,我想将其高度调整为 800。这会给我一个 some numberx800 的结果。反之亦然。

我的目标是根据我传递的 MAX-WIDTH 和 MAX-HEIGHT 调整图像大小。

类似这样的:

$image = new SimpleImage();
          $image->load($image_url); 
          $image->resizeToWidthHeight(MAX-WIDTH, MAX-HEIGHT);
          $image->save('photo.png');

这是我的 SimpleImage.php 类

<?php
    class SimpleImage {
     var $image;
     var $image_type;

     function load($filename) {
        $image_info = getimagesize($filename);
        $this->image_type = $image_info[2];
        if( $this->image_type == IMAGETYPE_JPEG ) {
            $this->image = imagecreatefromjpeg($filename);
        } elseif( $this->image_type == IMAGETYPE_GIF ) {
            $this->image = imagecreatefromgif($filename);
        } elseif( $this->image_type == IMAGETYPE_PNG ) {
            $this->image = imagecreatefrompng($filename);
        }
    }
    function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
        if( $image_type == IMAGETYPE_JPEG ) {
            imagejpeg($this->image,$filename,$compression);
        } elseif( $image_type == IMAGETYPE_GIF ) {
            imagegif($this->image,$filename);
        } elseif( $image_type == IMAGETYPE_PNG ) {
            imagepng($this->image,$filename);
        }
        if( $permissions != null) {
            chmod($filename,$permissions);
        }
    }

    function output($image_type=IMAGETYPE_JPEG) {
        if( $image_type == IMAGETYPE_JPEG ) {
            imagejpeg($this->image);
        } elseif( $image_type == IMAGETYPE_GIF ) {
            imagegif($this->image);
        } elseif( $image_type == IMAGETYPE_PNG ) {
            imageAlphaBlending($this->image, true);
            imageSaveAlpha($this->image, true);
            imagepng($this->image);
        }   
    }

    function getWidth() {
        return imagesx($this->image);
    }
    function getHeight() {
        return imagesy($this->image);
    }
    function resizeToHeight($height) {
        $ratio = $height / $this->getHeight();
        $width = $this->getWidth() * $ratio;
        $this->resize($width,$height);
    }
    function resizeToWidth($width) {
        $ratio = $width / $this->getWidth();
        $height = $this->getheight() * $ratio;
        $this->resize($width,$height);
    }
    function scale($scale) {
        $width = $this->getWidth() * $scale/100;
        $height = $this->getheight() * $scale/100;
        $this->resize($width,$height);
    }


    function resize($width,$height) {

        // ADDED CODE IS HERE - NOT SURE WHY IT DOESN'T WORK FOR PNG

        // Setup new image
        $new_image = imagecreatetruecolor($width, $height);
        // These parameters are required for handling PNG files.
        imagealphablending($new_image, false);
        imagesavealpha($new_image,true);
        $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
        imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);
        // Resize image
        imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
        $this->image = $new_image;
    }
}

?>

【问题讨论】:

    标签: php image resize png


    【解决方案1】:

    如果图像的尺寸发生变化:从 3:4 变为 16:9,则需要在某处应用一些裁剪。

    根据比例确定哪个维度最小,根据该属性调整大小,然后裁剪另一个维度。


    根据我的经验规模总是错误的。我建议您将其从库中删除。调整大小+裁剪是要走的路。

    你可以写一个类似cropTo($newX, $newY)的函数

    【讨论】:

    猜你喜欢
    • 2016-03-30
    • 2013-04-28
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多