Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将php脚本中的图像输出另存为文件_Php_Image - Fatal编程技术网

将php脚本中的图像输出另存为文件

将php脚本中的图像输出另存为文件,php,image,Php,Image,我尝试让php脚本将生成的图像输出为.jpg文件: $url = 'http://www.photopost.com/photopost/showfull.php?photo=7541'; file_put_contents('image.jpg',file_get_contents($url)); $page = file_get_contents($url); echo $page; 回波在浏览器中显示正确的图像。 但是image.jpg不会被保存 我如何才能做到这一点?您需要输出一个具

我尝试让php脚本将生成的图像输出为.jpg文件:

$url = 'http://www.photopost.com/photopost/showfull.php?photo=7541';
file_put_contents('image.jpg',file_get_contents($url));
$page = file_get_contents($url);
echo $page; 
回波在浏览器中显示正确的图像。 但是image.jpg不会被保存


我如何才能做到这一点?

您需要输出一个具有适当MIME类型的内容类型标题,以便浏览器能够理解服务器发送的文件类型

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

有关有效MIME类型的列表,请参阅和。

使用curl:-从url获取图像

$profile_Image = 'http://www.photopost.com/photopost/showfull.php?photo=7541'; //image url
$userImage = 'myimg.jpg'; // renaming image
$path = '';  // your saving path
$ch = curl_init($profile_Image);
$fp = fopen($path . $userImage, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
fclose($fp);
$profile_Image = 'http://www.photopost.com/photopost/showfull.php?photo=7541'; //image url
$userImage = 'myimg.jpg'; // renaming image
$path = '';  // your saving path
$thumb_image = file_get_contents($profile_Image);
if ($http_response_header != NULL) {
    $thumb_file = $path . $userImage;
    file_put_contents($thumb_file, $thumb_image);
}

使用文件获取内容从url获取图像:-

$profile_Image = 'http://www.photopost.com/photopost/showfull.php?photo=7541'; //image url
$userImage = 'myimg.jpg'; // renaming image
$path = '';  // your saving path
$ch = curl_init($profile_Image);
$fp = fopen($path . $userImage, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
fclose($fp);
$profile_Image = 'http://www.photopost.com/photopost/showfull.php?photo=7541'; //image url
$userImage = 'myimg.jpg'; // renaming image
$path = '';  // your saving path
$thumb_image = file_get_contents($profile_Image);
if ($http_response_header != NULL) {
    $thumb_file = $path . $userImage;
    file_put_contents($thumb_file, $thumb_image);
}
从更改您的url

http://www.photopost.com/photopost/showfull.php?photo=7541 //html document

然后使用其他答案中的代码或使用
imagecreatefromjpeg

我认为您需要设置标题<代码>标题('Content-Type:image/jpeg')。按照此urlini设置检查echo ini_get('allow_url_fopen')$url中的url=“”找不到图像$url=”;ini_get('allow_url_fopen');显示:1I尝试:代码是否已指定要保存图像的路径?(即
文件内容('/tmp/image.jpg',$data);
)默认路径可能无法由web服务器写入。web服务器/PHP进程可以写入该路径吗?您需要提供错误信息,以便我们能够提供帮助。Dave谢谢,这很有效!(我的选票因声誉不好而尚未公开)嗨,吉南德拉,坦斯克,谢谢你的帮助。此代码下载一个文件,在我的电脑上无法识别为有效的jpg!