当图像更大时,调整图像大小而不会丢失质量

时间:2017-06-24 08:54:55

标签: php image resize uploader

我在我的ftp上使用img上传器。 如果图像大到1024x768我使用脚本调整大小。 但是在调整大小后会失去质量。

来自uploader.php的代码:

                if(@move_uploaded_file($_FILES['myfile']['tmp_name'], "$upload_folder/" . $newname))
                if($width>1024 || $height>768) {
                    require './image_resize.php';
                    echo (image_resize("$upload_folder/" . $newname, "$upload_folder/" . $newname, 1024, 768));
                }

Code prom image_resize.php:

    <?php ini_set('memory_limit','500M');
function image_resize($src, $dst, $width, $height, $crop=0){

    if(!($pic = @getimagesize($src)))
        return false;

    $w = $pic[0];
    $h = $pic[1];
    $type = substr($pic['mime'], 6);

    $func = 'imagecreatefrom' . $type;

    if(!function_exists($func))
        return false;

    $img = $func($src);

    if($crop){

            if($w < $width && $h < $height)
                return false;

            $ratio = max($width/$w, $height/$h);
            $h = $height / $ratio;
            $x = ($w - $width / $ratio) / 2;
            $w = $width / $ratio;
    }
    else{

            if($w < $width && $h < $height)
                return false;

            $ratio = min($width/$w, $height/$h);
            $width = $w * $ratio;
            $height = $h * $ratio;
            $x = 0;
    }

    $new = imagecreatetruecolor($width, $height);

    if($type == "gif" || $type == "png"){
        imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
        imagealphablending($new, false);
        imagesavealpha($new, true);
    }
    imagecopyresampled($new, $img, 0, 0, $x, 0, $width, $height, $w, $h);

    $save = 'image' . $type;

    $save($new, $dst);
    return true;
}

在不损失质量的情况下缩小尺寸是不可能的:(

3 个答案:

答案 0 :(得分:1)

我找到了调整大小的脚本,工作正常。我认为这是我需要的:)

http://sanchiz.net/blog/resizing-images-with-php

但是,有一个问题,请为我更正:如果需要调整透明背景的PNG,这个脚本会删除透明并添加黑色背景。

在评论中发布了修改过的脚本,但是这段代码不能正常工作压缩功能

我需要修改此代码以调整大小PNG而不删除透明背景:

class SimpleImage {
public $image;
public $image_type;
public 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);
    }
}
public function save($filename, $image_type = IMAGETYPE_JPEG, $compression = 100, $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);
    }
}
public 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) {
        imagepng($this->image);
    }
}
public function resizeToHeight($height) {
    $ratio = $height / $this->getHeight();
    $width = $this->getWidth() * $ratio;
    $this->resize($width, $height);
}
public function getHeight() {
    return imagesy($this->image);
}
public function getWidth() {
    return imagesx($this->image);
}
public function resize($width, $height) {
    $new_image = imagecreatetruecolor($width, $height);
    imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
    $this->image = $new_image;
}
public function resizeToWidth($width) {
    $ratio  = $width / $this->getWidth();
    $height = $this->getheight() * $ratio;
    $this->resize($width, $height);
}
public function scale($scale) {
    $width  = $this->getWidth() * $scale / 100;
    $height = $this->getheight() * $scale / 100;
    $this->resize($width, $height);
}

谢谢

答案 1 :(得分:0)

由于默认API在图像处理方面可能不是很有效,我建议您使用库。看看grafika

答案 2 :(得分:0)

您还可以使用外部服务调整图像大小,例如此回购 Flyimg