Преобразование кода ImageMagick в GD (php) - PullRequest
1 голос
/ 18 июля 2011

Я хотел бы преобразовать некоторый PHP-код, который использует ImageMagick для обработки изображений.Я полностью новичок, когда дело доходит до использования 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.";

}
?>

Спасибо, что далипосмотри!

1 Ответ

2 голосов
/ 18 июля 2011

Вы обнаружите, что поддержка формата файлов GDs немного ограничена по сравнению с 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); 
...