图像调整大小不起作用(使用base64)

时间:2014-03-21 13:07:18

标签: php string image converter

以下代码应该在字符串中输出已调整大小的图像。不幸的是它只输出MQ ==所以我的错误在哪里?非常感谢帮助:)

<?php
// The file
$filename = 'tick.png';

// Set a maximum height and width
$width = 200;
$height = 200;

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefrompng($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

$newImage = imagepng($image_p, null, 100); // got in has to be from 0 to 9
echo base64_encode(file_get_contents($newImage)); // this still does not work°
?>

2 个答案:

答案 0 :(得分:3)

压缩级别不能为100.只能从0到9。

imagepng($image_p, null, from 0 to 9);

您可以使用Base64编码:

ob_start();
imagepng($image_p, null, 9); // got in has to be from 0 to 9
$stream = ob_get_clean();
echo base64_encode($stream);

答案 1 :(得分:3)

我就是这样做的:

      $Temp_Dump = $_FILES["file"]["tmp_name"];
      // Get new sizes
      list($width, $height) = getimagesize($Temp_Dump);
      $newwidth = 90;
      $newheight = 90;

      // Load
      $Temp_thumb = imagecreatetruecolor($newwidth, $newheight);
      //$source = imagecreatefromjpeg($Temp_Dump);

      if($extension == "jpg" OR $extension=='jpeg'){ 
      $source = ImageCreateFromJpeg($Temp_Dump); 
      }elseif ($extension == "gif"){ 
      $source = ImageCreateFromGIF($Temp_Dump); 
      }elseif ($extension == 'png'){
       $source = imageCreateFromPNG($Temp_Dump);
      }

      // Resize
      imagecopyresized($Temp_thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
相关问题