Resize images with PHP, support PNG, JPG

前端 未结 7 1391
囚心锁ツ
囚心锁ツ 2020-12-08 01:37

I am using this class:

class ImgResizer {

function ImgResizer($originalFile = \'$newName\') {
    $this -> originalFile = $originalFile;
}
function resiz         


        
7条回答
  •  不思量自难忘°
    2020-12-08 01:53

    Try this one and using this you can also save your image to specific path.

    function resize($file, $imgpath, $width, $height){
        /* Get original image x y*/
        list($w, $h) = getimagesize($file['tmp_name']);
        /* calculate new image size with ratio */
        $ratio = max($width/$w, $height/$h);
        $h = ceil($height / $ratio);
        $x = ($w - $width / $ratio) / 2;
        $w = ceil($width / $ratio);
    
        /* new file name */
        $path = $imgpath;
        /* read binary data from image file */
        $imgString = file_get_contents($file['tmp_name']);
        /* create image from string */
        $image = imagecreatefromstring($imgString);
        $tmp = imagecreatetruecolor($width, $height);
        imagecopyresampled($tmp, $image, 0, 0, $x, 0, $width, $height, $w, $h);
        /* Save image */
        switch ($file['type']) {
           case 'image/jpeg':
              imagejpeg($tmp, $path, 100);
              break;
           case 'image/png':
              imagepng($tmp, $path, 0);
              break;
           case 'image/gif':
              imagegif($tmp, $path);
              break;
              default:
              //exit;
              break;
            }
         return $path;
    
         /* cleanup memory */
         imagedestroy($image);
         imagedestroy($tmp);
    }
    

    Now you need to call this function while saving image as like...

    
    

提交回复
热议问题