PHP Save Image After imagecopyresampled

后端 未结 6 2259
执笔经年
执笔经年 2021-02-19 16:14
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_heig         


        
6条回答
  •  野的像风
    2021-02-19 16:58

    $filename = 'path/to/original/file.xxx'; // where xxx is file type (jpg, gif, or png)
    $newfilename = 'path/to/resized/file.xxx'; // where xxx is file type (jpg, gif, or png)
    $path_parts = pathinfo($filename);
    if ($path_parts['extension'] == 'jpg') {
        $image_p = imagecreatetruecolor($new_width, $new_height);
        $image = imagecreatefromjpeg($filename);
        imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
        imagejpeg($image_p, $newfilename);
    } elseif ($path_parts['extension'] == 'gif') {
        $image_p = imagecreatetruecolor($new_width, $new_height);
        $image = imagecreatefromgif($filename);
        imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
        imagegif($image_p, $newfilename);
    } elseif ($path_parts['extension'] == 'png') {
        $image_p = imagecreatetruecolor($new_width, $new_height);
        $image = imagecreatefrompng($filename);
        imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
        imagepng($image_p, $newfilename);
    } else {
            echo "Source file is not a supported image file type.";
    }
    

提交回复
热议问题