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

问题描述 投票:0回答:2

我正在尝试调整从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);
php image
2个回答
1
投票

用来调整$ 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;

0
投票

我做了一些改动,并以此方式使用它:

private function GenBase64Thumb($curl_image_data, $maxsize = 300, $quality = 85) {

$image = 'data://application/octet-stream;base64,' . base64_encode($curl_image_data);
$imagecreated = null;
$mime = getimagesize($image);

switch ($mime['mime']) {
  case 'image/jpeg':
  case 'image/jpg':
    $imagecreated = imagecreatefromjpeg($image);
    break;
  case 'image/png':
    $imagecreated = imagecreatefrompng($image);
    break;
  case 'image/gif':
    $imagecreated = imagecreatefromgif($image);
    break;
  default:
    return null;
}

if (!empty($imagecreated) && $imageScaled = imagescale($imagecreated, $maxsize)) {
  ob_start();
  switch ($mime['mime']) {
    case 'image/jpeg':
    case 'image/jpg':
      imagejpeg($imageScaled, NULL, $quality);
      break;
    case 'image/png':
      imagepng($imageScaled, NULL, "8");
      break;
    case 'image/gif':
      imagegif($imageScaled, NULL, $quality);
      break;
    default:
      return null;
  }
  $image = ob_get_contents();
  ob_end_clean();
  imagedestroy($imagecreated);
  return !empty($image) ? base64_encode($image) : null;
}
return null;

}


推荐问答