Change Image Size - PHP

前端 未结 5 1969
眼角桃花
眼角桃花 2020-12-10 08:52

I have recently created an upload function, but I don\'t know how to change the width and height to 75px... I tried one code I found through Google, but I just got this erro

相关标签:
5条回答
  • 2020-12-10 09:12

    Simple solutions some times changed alfa (transparent png). For me forks only this (it's real, just copy-pasted code)

    public static function resize($filename, $maxW, $maxH, $ext = null)
    {
        if ($ext === null)
            $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
        else
            $ext = strtolower($ext);
    
        list($origW, $origH) = getimagesize($filename);
    
        $w = $origW;
        $h = $origH;
    
        # taller
        if ($h > $maxH) {
            $w = ($maxH / $h) * $w;
            $h = $maxH;
        }
    
        # wider
        if ($w > $maxW) {
            $h = ($maxW / $w) * $h;
            $w = $maxW;
        }
    
    
        $resource = imagecreatetruecolor($w, $h);
        # MUST IMPORTANT TWO ROWS
        imagesavealpha($resource, true);
        imagefill($resource, 0, 0, 0x7fffffff);
    
        if ($ext == 'jpeg' || $ext == 'jpg') {
            $image = imagecreatefromjpeg($filename);
        } else if ($ext == 'png') {
            $image = imagecreatefrompng($filename);
        } else {
            throw new Exception('Unsupported extension');
        }
    
        imagecopyresampled(
            $resource,
            $image, 0, 0, 0, 0,
            $w, $h, $origW, $origH
        );
        imagedestroy($image);
    
        return $resource;
    }
    
    0 讨论(0)
  • 2020-12-10 09:25

    The Imagick Class is not found, because it is a PHP extension you need to install on your server.

    Read the following documentation to find instructions on how to use/install the extension. http://www.php.net/manual/en/book.imagick.php

    0 讨论(0)
  • 2020-12-10 09:25

    You can also scale the image client-side, e.g. using plupload library (can resize JPEG and PNG).

    0 讨论(0)
  • 2020-12-10 09:28

    I've successfully used GD to do this recently, specifically using the imagecopyresampled function.


    To expand a little on that... Once I had the image uploaded (which I wont go into, because that is a whole other issue), I did something fairly simple like this:

    $original_info = getimagesize($filename);
    $original_w = $original_info[0];
    $original_h = $original_info[1];
    $original_img = imagecreatefromjpg($filename);
    $thumb_w = 100;
    $thumb_h = 100;
    $thumb_img = imagecreatetruecolor($thumb_w, $thumb_h);
    imagecopyresampled($thumb_img, $original_img,
                       0, 0,
                       0, 0,
                       $thumb_w, $thumb_h,
                       $original_w, $original_h);
    imagejpeg($thumb_img, $thumb_filename);
    imagedestroy($thumb_img);
    imagedestroy($original_img);
    

    Please note that I have not tested this code. It's just here to give you a basic idea of my method.

    0 讨论(0)
  • 2020-12-10 09:32

    I use code like this:

    $t = imagecreatefromjpeg($old_path);
    $x = imagesx($t);
    $y = imagesy($t);
    
    $s = imagecreatetruecolor($new_width, $new_height);
    
    imagecopyresampled($s, $t, 0, 0, 0, 0, $new_width, $new_height,
            $x, $y);
    
    imagejpeg($s, $new_path);
    chmod($new_path, 0644);
    
    0 讨论(0)
提交回复
热议问题