【问题标题】:Image resizing not working (using base64)图像大小调整不起作用(使用 base64)
【发布时间】:2014-04-28 20:59:00
【问题描述】:

以下代码应该在字符串中输出调整大小的图像。不幸的是它只输出 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°
?>

【问题讨论】:

    标签: php string image converter


    【解决方案1】:

    压缩级别不能为 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);
    

    【讨论】:

    • echo base64_encode(file_get_contents($newImage));不工作
    【解决方案2】:

    这就是我的做法:

          $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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-13
      • 2014-08-29
      • 2014-05-12
      • 1970-01-01
      相关资源
      最近更新 更多