Already all day I am looking for a very basic crop and upload script, but apperntly that doesn’t exist in PHP unless you use Imagemagick as mentioned by Rubble . For example. I am quite used to use Coldfusion. If I need to create a normal photo file and a thumbnail the following code will make this happen:
<cfset thumbnailPath = expandpath('../slideshow_photos/thumbnails')>
<cfset photoPath = expandpath('../slideshow_photos/photos')>
<cffile action="upload" filefield="fileUpload" destination="#photoPath#" nameconflict="makeunique" accept="image/*" mode="777">
<cfimage name="uploadedImage" source="#photoPath#/#file.serverFile#" >
<cfimage action="resize" width="" height="80" source="#uploadedImage#" destination="#thumbnailPath#/#file.serverFile#" overwrite="true">
<cfif uploadedImage.width gt photoWidth AND uploadedImage.height gt photoHeight>
<cfimage action="resize" width="" height="550" source="#uploadedImage#" destination="#photoPath#/#file.serverFile#" overwrite="true"/>
</cfif>
Where in PHP I need something like this to reach nearly the same result:
<?php
define('THUMBNAIL_IMAGE_MAX_HEIGHT', 80);
define('PHOTO_IMAGE_MAX_HEIGHT', 550);
function generate_image_thumbnail($source_image_path, $thumbnail_image_path) {
list($source_image_width, $source_image_height, $source_image_type) = getimagesize($source_image_path);
switch ($source_image_type) {
case IMAGETYPE_GIF:
$source_gd_image = imagecreatefromgif($source_image_path);
break;
case IMAGETYPE_JPEG:
$source_gd_image = imagecreatefromjpeg($source_image_path);
break;
case IMAGETYPE_PNG:
$source_gd_image = imagecreatefrompng($source_image_path);
break;
}
if ($source_gd_image === false) {
return false;
}
$source_aspect_ratio = $source_image_width / $source_image_height;
$photo_aspect_ratio = THUMBNAIL_IMAGE_MAX_WIDTH / THUMBNAIL_IMAGE_MAX_HEIGHT;
if ($source_image_width <= THUMBNAIL_IMAGE_MAX_WIDTH && $source_image_height <= THUMBNAIL_IMAGE_MAX_HEIGHT) {
$thumbnail_image_width = $source_image_width;
$thumbnail_image_height = $source_image_height;
} elseif ($thumbnail_aspect_ratio > $source_aspect_ratio) {
$thumbnail_image_width = (int) (THUMBNAIL_IMAGE_MAX_HEIGHT * $source_aspect_ratio);
$thumbnail_image_height = THUMBNAIL_IMAGE_MAX_HEIGHT;
} else {
$thumbnail_image_width = THUMBNAIL_IMAGE_MAX_WIDTH;
$thumbnail_image_height = (int) (THUMBNAIL_IMAGE_MAX_WIDTH / $source_aspect_ratio);
}
$thumbnail_gd_image = imagecreatetruecolor($thumbnail_image_width, $thumbnail_image_height);
imagecopyresampled($thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height);
imagejpeg($thumbnail_gd_image, $thumbnail_image_path, 90);
imagedestroy($source_gd_image);
imagedestroy($thumbnail_gd_image);
return true;
}
function generate_image_photo($source_image_path, $photo_image_path) {
list($source_image_width, $source_image_height, $source_image_type) = getimagesize($source_image_path);
switch ($source_image_type) {
case IMAGETYPE_GIF:
$source_gd_image = imagecreatefromgif($source_image_path);
break;
case IMAGETYPE_JPEG:
$source_gd_image = imagecreatefromjpeg($source_image_path);
break;
case IMAGETYPE_PNG:
$source_gd_image = imagecreatefrompng($source_image_path);
break;
}
if ($source_gd_image === false) {
return false;
}
$source_aspect_ratio = $source_image_width / $source_image_height;
$photo_aspect_ratio = PHOTO_IMAGE_MAX_WIDTH / PHOTO_IMAGE_MAX_HEIGHT;
if ($source_image_width <= PHOTO_IMAGE_MAX_WIDTH && $source_image_height <= PHOTO_IMAGE_MAX_HEIGHT) {
$photo_image_width = $source_image_width;
$photo_image_height = $source_image_height;
} elseif ($photo_aspect_ratio > $source_aspect_ratio) {
$photo_image_width = (int) (PHOTO_IMAGE_MAX_HEIGHT * $source_aspect_ratio);
$photo_image_height = PHOTO_IMAGE_MAX_HEIGHT;
} else {
$photo_image_width = PHOTO_IMAGE_MAX_WIDTH;
$photo_image_height = (int) (PHOTO_IMAGE_MAX_WIDTH / $source_aspect_ratio);
}
$photo_gd_image = imagecreatetruecolor($photo_image_width, $photo_image_height);
imagecopyresampled($photo_gd_image, $source_gd_image, 0, 0, 0, 0, $photo_image_width, $photo_image_height, $source_image_width, $source_image_height);
imagejpeg($photo_gd_image, $photo_image_path, 90);
imagedestroy($source_gd_image);
imagedestroy($photo_gd_image);
return true;
}
define('UPLOADED_IMAGE_DESTINATION', '../gallery/');
define('THUMBNAIL_IMAGE_DESTINATION', '../gallery/thumbnails/');
define('PHOTO_IMAGE_DESTINATION', '../gallery/photos/');
function process_image_upload($field){
$temp_image_path = $_FILES[$field]['tmp_name'];
$temp_image_name = $_FILES[$field]['name'];
list(, , $temp_image_type) = getimagesize($temp_image_path);
if ($temp_image_type === NULL) {
return false;
}
switch ($temp_image_type) {
case IMAGETYPE_GIF:
break;
case IMAGETYPE_JPEG:
break;
case IMAGETYPE_PNG:
break;
default:
return false;
}
$uploaded_image_path = UPLOADED_IMAGE_DESTINATION . $temp_image_name;
move_uploaded_file($temp_image_path, $uploaded_image_path);
$thumbnail_image_path = THUMBNAIL_IMAGE_DESTINATION . preg_replace('{\\\\.[^\\\\.]+$}', '.jpg', $temp_image_name);
$photo_image_path = PHOTO_IMAGE_DESTINATION . preg_replace('{\\\\.[^\\\\.]+$}', '.jpg', $temp_image_name);
$res_thumbnail = generate_image_thumbnail($uploaded_image_path, $thumbnail_image_path);
$res_photo = generate_image_photo($uploaded_image_path, $photo_image_path);
return $res_thumbnail ? array($uploaded_image_path, $thumbnail_image_path) : false;
return $res_photo ? array($uploaded_image_path, $photo_image_path) : false;
}
$res_thumbnail = process_image_upload('Image1');
$res_photo = process_image_upload('Image1');
?>
Is there really not a more simple sollution to just crop and upload an image using PHP?