将 ImageMagick 代码转换为 GD (php)

发布于 2024-11-24 16:00:05 字数 1389 浏览 3 评论 0原文

我想转换一些使用 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.";

}
?>

感谢您的查看!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

意中人 2024-12-01 16:00:06

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

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

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); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文