如何创建一个脚本,该脚本将打开存储在服务器上的文件,复制其内容并导出/下载为。jpg文件到客户端。这个代码正确吗?
<?php
$imgfile = fopen(image/location.jpg);
$loadimg = imagecreatefromjpeg($imgfile);
imagejpeg($loadimg);
?>
或者我应该用另一种方式?我是php中操作图像文件的新手,所以这就是为什么这段代码可能看起来很奇怪。
我想做一个脚本,允许创建干净的图像文件的副本,但不是整个文件,只有它包含的图像,没有图像文件可以存储的额外数据。这就是为什么我最初的想法是使用GD打开文件进行编辑并将其复制到新图像中。
imagecreatefromjpeg()接受字符串(文件路径)作为参数,而不是文件指针。
<?php
$srcpath = 'image/location.jpg';
if ( !is_readable($srcpath) ) {
header('file not found', 404);
die;
}
else {
header('Content-Type: image/jpeg');
// for affecting the suggested file name
// see https://tools.ietf.org/html/rfc2616#section-19.5.1
$loadimg = imagecreatefromjpeg($imgfile);
if ( !$loadimg ) {
header('unable to process resource', 404); // 404 is not the correct error code for this....
die;
}
imagejpeg($loadimg);
}