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); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器