![](/img/trans.png)
[英]Warning: file_get_contents(image.jpg) [function.file-get-contents]: failed to open stream: No such file or directory in php
[英][php]Load image file to open its content and export as .jpg file
如何创建一个脚本来打开存储在服务器上的文件,复制其内容并以.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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.