PHP调整图像大小&存放在BLOB&通过base64_encode访问

时间:2016-12-19 12:24:37

标签: php base64 gd image-resizing

在图片上传时,我想将图像调整为400 X 400比例。我使用GD库代码:

$profilePicture = $_FILES['imgProfilePicture']['tmp_name'];     
                // Get new dimensions
        list($width_orig, $height_orig, $type) = getimagesize($profilePicture);
        $ratio_orig = $width_orig/$height_orig;

        if ($intWidth/$intHeigth > $ratio_orig) {
        $intWidth = $intHeigth*$ratio_orig;
        } else {
        $intHeigth = $intWidth/$ratio_orig;
        }

        // Resample
         $thumb = imagecreate($intWidth, $intHeigth); 
        ob_start();
        $extension = image_type_to_extension($type); 
        if($extension == ".jpg" OR $extension=='.jpeg'){  
                  $source = ImageCreateFromJpeg($profilePicture); 
                  $newImage = imagejpeg($thumb, null, 9); 
                  }elseif ($extension == ".gif"){ 
                  $source = ImageCreateFromGIF($profilePicture); 
                  $newImage = imagegif($thumb, null, 9); 
                  }elseif ($extension == '.png'){
                   $source = imageCreateFromPNG($profilePicture);
                   $newImage = imagepng($thumb, null, 9); 
                  }                        

        imagecopyresized($thumb, $source, 0, 0, 0, 0, $intWidth, $intHeigth, $width_orig, $height_orig);

        $stream = ob_get_clean();
        $imgProfilePicture =  file_get_contents($stream);
        $sql="update `".$doctorTableName."` set`imgProfilePicture`='".$imgProfilePicture."' where id='".$id."'";            
        $wpdb->query($sql);

&安培;我使用此代码显示图像:

  <img width="70" height="70" src="data:image/jpeg;base64, '.base64_encode($doctor->imgProfilePicture).'  "/>

但我收到的错误是: 警告:file_get_contents()期望参数1是有效路径,给定字符串

1 个答案:

答案 0 :(得分:0)

我在用户贡献 scaleImageFileToBlob()

下的此链接上获得了此代码
 http://php.net/manual/en/function.imagejpeg.php