PHP从内存中的curl调整图像大小

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (55)

我正在尝试调整我从Curl请求中获取的图像,但我不想将其保存到文件中以调整其大小。这是我正在尝试但不工作:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $remote_file);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$picture = curl_exec($ch);
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
curl_close($ch);

// And then to resize $picture
$data = htmlspecialchars($picture, ENT_QUOTES);
$data = base64_decode($data);
$im = imagecreatefromstring($data); 
$imaget1 = imagescale($im, $maxsize);
提问于
用户回答回答于

有效的是调整$ picture的大小:

$image = 'data://application/octet-stream;base64,' . base64_encode($picture);

$mime = getimagesize($image);

if($mime['mime'] == 'image/jpeg'){ $imagecreated = imagecreatefromjpeg($image); }
if($mime['mime'] == 'image/jpg'){ $imagecreated = imagecreatefromjpeg($image); }
if($mime['mime'] == 'image/png'){ $imagecreated = imagecreatefrompng($image); }
if($mime['mime'] == 'image/gif'){ $imagecreated = imagecreatefromgif($image); }
$imageScaled = imagescale($imagecreated, $maxsize);

ob_start ();
if($mime['mime'] == 'image/jpeg'){ imagejpeg($imageScaled, null, $quality); }
if($mime['mime'] == 'image/jpg'){ imagejpeg($imageScaled, null, $quality); }
if($mime['mime'] == 'image/png'){ imagepng($imageScaled, null, "8"); }
if($mime['mime'] == 'image/gif'){ imagegif($imageScaled, null, $quality); }
$image = ob_get_contents ();
ob_end_clean ();

imagedestroy($imagecreated);

return $image;

扫码关注云+社区

领取腾讯云代金券