php 使用 imagejpeg 保存和提供图像文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4375302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:52:53  来源:igfitidea点击:

Using imagejpeg to save & serve image file

phpimage-processing

提问by V. Arora

I'm doing a bit of an experiment with PHP + Image manipulation. I'm trying to convert some images into black and white versions. I'm mostly figured it out but have one slight issue.

我正在做一些 PHP + 图像操作的实验。我正在尝试将一些图像转换为黑白版本。我主要是想通了,但有一个小问题。

In order to reduce the strain on the server, I wanted to save the B&W versions and only run the image filtering on images that haven't been run through the script before. So, I have something like this:

为了减少服务器的压力,我想保存黑白版本,只对之前没有通过脚本运行的图像运行图像过滤。所以,我有这样的事情:

<?php 
header("Content-type: image/jpeg");

$file = $_GET['img'];
$name = md5($file).".jpg";

if(file_exists("/path/to/file" . $name)) {

    ob_clean();
    flush();
    readfile("path/to/file" . $name);
    exit;

}
else {

 $image = imagecreatefromjpeg($file);

 imagefilter($image, IMG_FILTER_GRAYSCALE);
 imagejpeg($image, "/path/to/file" . $name);

 imagedestroy($image);
};

?> 

This does create the B&W versions of the file and save them to the server. The initial "if" statement is also working - it correctly serves the image if it already exists.

这会创建文件的黑白版本并将它们保存到服务器。最初的“if”语句也有效 - 如果图像已经存在,它会正确地提供图像。

The issue is that for new images that are run through, this saves them but doesn't output them to the browser. What can I use/change in order to do that?

问题在于,对于运行的新图像,这会保存它们但不会将它们输出到浏览器。我可以使用/更改什么来做到这一点?

Also, this is my first time doing anything like this. Any general tips you have about doing the above would be appreciated.

此外,这是我第一次做这样的事情。您对执行上述操作的任何一般提示将不胜感激。

回答by Stoic

A compact and correct form for above function can be:

上述函数的紧凑且正确的形式可以是:

<?php 
header("Content-type: image/jpeg");

$file = $_GET['img'];
$name = md5($file).".jpg";

if(!file_exists("/path/to/file" . $name)) {
 imagefilter($image, IMG_FILTER_GRAYSCALE);
 imagejpeg($image, "/path/to/file" . $name);
} else {
 $image = imagecreatefromjpeg("/path/to/file" . $name);
}

imagejpeg($image);
imagedestroy($image);

?> 

回答by thedom

Because you are saving the image with imagejpeg()in the elsepart, your image will not be shown. So you have to add

因为您imagejpeg()else零件中保存图像,所以不会显示您的图像。所以你必须添加

readfile("/path/to/file". $name);

after imagedestroy();).

之后imagedestroy();)。

回答by Mike

You could wrap the image-output code in a function - something like this (untested):

您可以将图像输出代码包装在一个函数中 - 像这样(未经测试):

function output_image ( $image_file ) {
    header("Content-type: image/jpeg");
    header('Content-Length: ' . filesize($image_file));
    ob_clean();
    flush();
    readfile($image_file);
}

$file = $_GET['img'];
$name = md5( $file ) . ".jpg";
$image_file = "/path/to/file/" . $name;

if(!file_exists( $image_file )) {

   $image = imagecreatefromjpeg( $file );
   imagefilter( $image, IMG_FILTER_GRAYSCALE );
   imagejpeg( $image, $image_file );
   imagedestroy( $image );

}

output_image( $image_file );