【问题标题】:PHP save a resized imagePHP保存调整大小的图像
【发布时间】:2017-08-10 08:40:43
【问题描述】:

我有这个 PHP 脚本可以调整图像大小并显示结果图像:

$filename = 'my-image.jpeg';

// Content type
header('Content-Type: image/jpeg');

// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = 1200;
$new_height = ceil($height * ($new_width/$width));

// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output
imagejpeg($image_p, null, 100);

现在我想保存图片而不是显示它,保留原图,并将新创建的图片命名为“my-image-resized.jpeg”,如何?

【问题讨论】:

    标签: php image-processing gd image-resizing


    【解决方案1】:

    imagejpeg function reference所述,函数定义为:

    bool imagejpeg ( resource $image [, mixed $to [, int $quality ]] )
    

    第二个参数(如$to),描述如下:

    保存文件的路径或打开的流资源(在此函数返回后自动关闭)。如果不设置或为NULL,则直接输出原始图像流。

    因为您已经将NULL 传递给$to 参数,所以它是直接流式传输的,即显示。为了将它保存到文件系统,你应该这样做:

    imagejpeg($image_p, 'sampleImage.jpg' , 100);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-24
      • 1970-01-01
      • 2011-02-18
      • 2010-10-12
      • 1970-01-01
      • 1970-01-01
      • 2014-04-14
      • 1970-01-01
      相关资源
      最近更新 更多