Skip to content

Instantly share code, notes, and snippets.

@apocratus
Created June 9, 2011 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save apocratus/1016879 to your computer and use it in GitHub Desktop.
Save apocratus/1016879 to your computer and use it in GitHub Desktop.
resize image and create thumbnail (fixed width and height)
<?php
// Get the image and create a thumbnail
$img = imagecreatefromjpeg($_FILES["Filedata"]["tmp_name"]);
if (!$img) {
echo "ERROR:could not create image handle ". $_FILES["Filedata"]["tmp_name"];
exit( 0 );
}
$width = imageSX($img);
$height = imageSY($img);
if (!$width || !$height) {
echo "ERROR:Invalid width or height";
exit(0);
}
// Portrait or landscape image?
if($width > $height) {
$image_mode = 'landscape';
}
elseif($width < $height) {
$image_mode = 'portrait';
}
else {
$image_mode = 'landscape';
}
// Target sizes
$target_width = 200;
$target_height = 270;
$target_thumb_width = 88;
$target_thumb_height = 84;
// Calculate ratio of uploaded image
$target_ratio = $target_width / $target_height;
$target_thumb_ratio = $target_thumb_width / $target_thumb_height;
$img_ratio = $width / $height;
// calculate zwarte balken, onder of boven en hoe groot
if($image_mode == 'portrait') {
if($img_ratio < $target_ratio) {
//zwarte balken aan weerszijden
$resize_ratio = $target_height / $height;
$new_width = $width * $resize_ratio;
// calculate position of resized image in target size
$blackness = $target_width - $new_width;
$left_start = $blackness/2;
$leftstart = round($left_start);
$topstart = 0;
$target_width = $new_width;
// thumb resizen
$resize_thumb_ratio = $target_thumb_height / $height;
$new_thumb_width = $width * $resize_thumb_ratio;
$blackness = $target_thumb_width - $new_thumb_width;
$left_thumb_start = $blackness/2;
$leftthumbstart = round($left_thumb_start);
$topthumbstart = 0;
$target_thumb_width = $new_thumb_width;
}
else {
// zwarte balken boven en onder
$image_mode = 'landscape';
}
}
if($image_mode == 'landscape') {
// zwarte balken boven en onder
if($img_ratio > $target_ratio) {
// Berekeningen
$resize_ratio = $target_width / $width;
$new_height = $height * $resize_ratio;
// calculate position of resized image in target size
$blackness = $target_height - $new_height;
$top_start = $blackness/2;
$topstart = round($top_start);
$leftstart = 0;
$target_height = $new_height;
// thumb resizen
$resize_thumb_ratio = $target_thumb_width / $width;
$new_thumb_height = $height * $resize_thumb_ratio;
$blackness = $target_thumb_height - $new_thumb_height;
$top_thumb_start = $blackness/2;
$topthumbstart = round($top_thumb_start);
$leftthumbstart = 0;
$target_thumb_height = $new_thumb_height;
}
}
// create 2 black (empty) images
$new_img = ImageCreateTrueColor(200, 270);
$new_thumb = ImageCreateTrueColor(88, 84);
// place resized image on black canvas
if (!@imagecopyresampled($new_img, $img, $leftstart, $topstart, 0, 0, $target_width, $target_height, $width, $height)) {
echo "ERROR:Could not resize image";
exit(0);
}
// place resized thumbnail on black canvas
if (!@imagecopyresampled($new_thumb, $img, $leftthumbstart, $topthumbstart, 0, 0, $target_thumb_width, $target_thumb_height, $width, $height)) {
echo "ERROR:Could not resize image";
exit(0);
}
if (!isset($_SESSION["file_info"])) {
$_SESSION["file_info"] = array();
}
// name of the new image
$file_id = $this->userId;
// Use a output buffering to load the image into a variable
imagejpeg( $new_img , 'assets/dynamic/images/'.$file_id.'.jpg' , 100 );
imagejpeg( $new_thumb , 'assets/dynamic/images/thumb_'.$file_id.'.jpg' , 100 );
echo $file_id; // Return the file id to the script
// EOF
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment