duanjia2772 2011-07-18 08:23
浏览 51
已采纳

将ImageMagick代码转换为GD(php)

I'd like to convert some PHP code that uses ImageMagick for image processing. I am a total newbie when it comes to using GD but I hope I could get some directions or code suggestions.

The current PHP code can be seen below

$rand = rand();
$galleryWidth ='245';
$galleryHeight ='245';

$result = array();

if (isset($_FILES['photoupload']) )
{
    $file = $_FILES['photoupload']['tmp_name'];
    $error = false;
    $size = false;



        list($file_name, $file_type) = split('[.]', $_FILES["photoupload"]["name"]);

       move_uploaded_file($_FILES["photoupload"]["tmp_name"],
      "./photos/org/".$rand.'.'.$file_type);

        list($width,$height)=getimagesize('./photos/org/'. $rand.'.'.$file_type);


        if(($galleryWidth/$width) < ($galleryHeight/$height)){  
        exec("C:/imagemagick/convert ./photos/org/". $rand.".".$file_type."\
            -thumbnail ".round(($width*($galleryWidth/$width)), 0)."x".round(($height*($galleryWidth/$width)), 0)." \
            -quality 90   ./photos/".$_GET['id'].".jpg");
        }
        else{
        exec("C:/imagemagick/convert ./photos/org/". $rand.".".$file_type."\
            -thumbnail ".round(($width*($galleryHeight/$height)), 0)."x".round(($height*($galleryHeight/$height)), 0)." \
            -quality 90   ./photos/".$_GET['id'].".jpg");
        }
        $result['result'] = 'success';
        $result['size'] = "Uploaded an image ({$size['mime']}) with  {$size[0]}px/{$size[1]}px.";

}
?>

Thanks for having a look at it!

  • 写回答

1条回答 默认 最新

  • dqe55175 2011-07-18 08:46
    关注

    You'll find GDs file format support is a bit limited compared to ImageMagick's, but you're looking for something similar to the following.

    $inputPath = "./photos/org/{$rand}.{$file_type}";
    $outputPath = "./photos/{$imageId}.jpg";
    
    list($old_width, $old_height) = getimagesize($inputPath);
    
    // -- Calculate the new_width and new_height here, however you want to.
    $new_width = 250;
    $new_height = 250;
    
    // -- Initialise the source image container
    if( $file_type == 'png' )
        $src_img = imagecreatefrompng($inputPath);
    else if( $file_type == 'jpeg' || $file_type == 'jpg' )
        $src_img = imagecreatefromjpeg($inputPath);
    else
        throw new Exception("Unsupported file format.");
    
    // -- Prepare the new image container
    $dst_img = ImageCreateTrueColor($new_width, $new_height);
    
    // -- Resample the "old" image to the "new" image
    imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height); 
    
    // -- Save the new canvas (the 90 represents the quality percentage)
    imagejpeg($dst_img, $outputPath, 90); 
    
    // -- Perform cleanup on image containers.
    imagedestroy($dst_img); 
    imagedestroy($src_img); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系