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