将ImageMagick代码转换为GD(php)

将ImageMagick代码转换为GD(php),php,image-processing,imagemagick,gd,Php,Image Processing,Imagemagick,Gd,我想转换一些使用ImageMagick进行图像处理的PHP代码。当谈到使用GD时,我是一个完全的新手,但我希望我能得到一些指导或代码建议 下面可以看到当前的PHP代码 $rand = rand(); $galleryWidth ='245'; $galleryHeight ='245'; $result = array(); if (isset($_FILES['photoupload']) ) { $file = $_FILES['photoupload']['tmp_name']

我想转换一些使用ImageMagick进行图像处理的PHP代码。当谈到使用GD时,我是一个完全的新手,但我希望我能得到一些指导或代码建议

下面可以看到当前的PHP代码

$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.";

}
?>
$rand=rand();
$galleryWidth='245';
$galleryHeight='245';
$result=array();
如果(isset($_文件['photoupload']))
{
$file=$\u文件['photoupload']['tmp\u name'];
$error=false;
$size=false;
列表($file_name,$file_type)=拆分(“[.]”,$_文件[“photoupload”][“name”]);
移动上传的文件($文件[“照片上传”][“tmp文件名”],
“/photos/org/”$rand.“.$file\u type);
列表($width,$height)=getimagesize('./photos/org/'.$rand.'.$file_type);
如果(($galleryWidth/$width)<($galleryHeight/$height)){
exec(“C:/imagemagick/convert./photos/org/”$rand.“.”$file\u type。“\
-缩略图“.round($width*($galleryWidth/$width)),0)。“x”.round($height*($galleryWidth/$width)),0)。”\
-质量90./photos/“$_GET['id'..”.jpg”);
}
否则{
exec(“C:/imagemagick/convert./photos/org/”$rand.“.”$file\u type。“\
-缩略图“.round($width*($galleryHeight/$height)),0.“x.”round($height*($galleryHeight/$height)),0.“\
-质量90./photos/“$_GET['id'..”.jpg”);
}
$result['result']='success';
$result['size']=“上载了一个带有{$size[0]}px/{$size[1]}px的图像({$size['mime']})。”;
}
?>

谢谢你看一看

与ImageMagick相比,您会发现GDs文件格式支持有点有限,但您正在寻找类似于以下内容的支持

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

没有通用的“转换”技术。请告诉我们你想要实现什么。很难从你的代码中理解。对不起。基本上,这是一个图像上传表单的PHP代码。为了调整大小和图像质量下降的工作,我将需要ImageMagick。“-thumbnail”和“-quality”不是PHP代码,而是ImageMagick代码。据我所知,GD具有相似的特性,但语法不同。最重要的是创建缩略图,使其具有正确的比例。有什么原因要切换回GD吗?我建议您切换到,而不是执行此“exec()”黑客操作。您可以获得相同的功能,但代码将是跨平台和更干净的。不幸的是,我没有让imagemagick工作。internet上有一些可用的说明,但imagemagick扩展名根本没有出现在phpinfo()中。我以前在专业网站管理员上问过这个问题,并在我验证它是否有效之前将该问题标记为已回答:(是的。@nctrnl,您的ImageMagick调用只是将图像缩放到更小的大小,因此,
imagecopyresampled
无疑是您脑海中浮现的GD函数。如果您将来有更复杂的图像缩略图要求,请查看。我在尝试上载jpg文件时遇到此错误:警告:getimagesize(./photos/org/1713.[function.getimagesize]:无法打开流:在第6行的C:\xxx\xxx\xxx\xxx\employees\upload\u foto.php中没有这样的文件或目录致命错误:未捕获异常“exception”,消息为“Unsupported file format”。在C:\xxx\xxx\xxx\xxx\xxx\employees\upal\u foto.php中:18堆栈跟踪:#0{main}在C:\xxx\xxx\xxx\xxx\xxx\employees\upload\u foto.php在线中抛出18@nctrnl听上去,因为您的
$inputPath
没有设置为图像进入时的路径。请更正此错误,您应该会看到此错误消失。