Image creating functions not working on https

时间:2016-10-20 13:05:21

标签: php image


I am working on project on php and created a module for cropping a image. My code for cropping image is:

$targ_w = 400;
$targ_h = 400;
$jpeg_quality = 90;
$src = "PATH_OF_ORIGINAL_IMAGE";
$filename = pathinfo($src);
$extension = $filename['extension'];
if($extension == 'jpg' || $extension == 'jpeg')
{
	$img_r = imagecreatefromjpeg($src);
}
if($extension == 'png')
{
	$img_r = imagecreatefrompng($src);
}
if($extension == 'gif')
{
	$img_r = imagecreatefromgif($src);
}
$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
$imageName = "cropped.jpeg";
imagecopyresampled($dst_r,$img_r,0,0,600,600,$targ_w,$targ_h,600,600);
$path = "PATH_FOR_NEW_IMAGE".$imageName;
header('Content-type: image/jpeg');
imagejpeg($dst_r,$path,$jpeg_quality);

This code is working great and cropping image with provided details. But I had moved my website from http to https, This code stops working. No error is displaying and creating image. But images are blank and black image. When I moved website back to http it's again starts working. But I want my website to be on https. I have two questions and they are:

  1. Why is the created image blank?
  2. If it's server issue how can I solve this?

2 个答案:

答案 0 :(得分:1)

查看您的回答我认为您正在从网址加载$src

我会在操作之前尝试下载文件,如下所示:

$context = stream_context_create(["ssl" => [
  "verify_peer"      => false,
  "verify_peer_name" => false]
]);

$fileContent = file_get_contents($src, false, $context);

然后您可以使用以下方式创建图像:

$img_r = imagecreatefromstring($fileContent);

如果不检查扩展名,它将返回有效的图像资源,如果字符串的内容不是图像,则返回false。 使用此方法可以避免在使用jpg扩展名重命名png图像时发生错误。

注意您正在禁用ssl检查,这可能是一个安全漏洞。

答案 1 :(得分:0)

您是否考虑通过服务处理裁剪(可能还有其他图像处理操作),该服务也可以通过快速CDN使用HTTPS为您的用户提供处理后的图像?例如http://cloudinary.com/documentation/php_integration#display_and_manipulate_images