1) { /* STEP 2: Create your filename and get the uploaded image */ /************************************************************************/ $timestamp = time(); $resource = $_FILES['form_upload_field_name']['tmp_name']; // The uploaded filename // $image_destination = '/path/to/images/folder/'.$timestamp.'.jpg'; $thumb_destination = '/path/to/thumbs/folder/'.$timestamp.'.jpg'; // this helps prevent duplication ie. johnsmith.jpg // /* STEP 3: Check against valid image types */ /************************************************************************/ /* Image type handler */ switch(TRUE) { // HANDLES VARIOUS IMAGETYPES, NOT JUST JPEG, FOR USE WITH LOGOS ETC. case (stripos($resource,'.gif',0)) :$image = imagecreatefromgif($resource); break; case (stripos($resource,'.jpg',0) || stripos($resource,'.jpeg',0)) :$image = imagecreatefromjpeg($resource); break; case (stripos($resource,'.png',0)) :$image = imagecreatefrompng($resource); break; } /* STEP 4: Get the original size and set your thumbnail size */ /************************************************************************/ // Set a colour for the resouce (background default is black) $white = imagecolorallocate($image, 255, 255, 255); // X & Y Coordinates $srcsize = getimagesize($resource); $orig_width = $srcsize[0]; $orig_height = $srcsize[1]; $new_width = 100; // Your desired x $new_height = 100; // Your desired y /* STEP 5A: CROP THE IMAGE */ /************************************************************************/ $dest_img = imagecreatetruecolor($new_width,$new_height); $white = imagecopyresampled($dst_img, 255,255,255); ImageFilledRectangle($destImg,0,0,$new_width,$new_height,$white); $xm = $orig_width / $new_width; $ym = $orig_height / $new_height; $yHeight = $new_height / 2; $xWidth = $new_width / 2; if($orig_width > $orig_height) { // A landscape image // REMOVE PIXELS WIDTH ASPECT $xAdjusted = $orig_width / $ym; $xHalf = $xAdjusted / 2; $xInt = $xHalf - $xWidth; ImageCopyResampled($destImg,$srcImg,-$xInt,0,0,0,$xAdjusted,$new_height,$x,$y); } elseif(($orig_width < $orig_height) || ($orig_width == $orig_height)) { // a portrait or square image // REMOVE PIXELS HEIGHT ASPECT $yAdjusted = $orig_height / $xm; $yHalf = $yAdjusted / 2; $yInt = $yHalf - $yHeight; $ySrc = $yInt / 2; // TRIAL SOME ARITHMETIC TO TRY AND IMPROVE Y CROPPING ImageCopyResampled($destImg,$srcImg,0,-$ySrc,0,0,$new_width,$yAdjusted,$x,$y); } else { // no scaling required // // STRAIGHTFORWARD RESIZE ImageCopyResampled($destImg,$srcImg,0,0,0,0,$new_width,$new_height,$x,$y); } /* STEP 5B: ROTATE THE IMAGE (REMOVE THIS TO USE AS CROP ONLY) */ /************************************************************************/ // Rotate the image (clockwise 0 - 359) $rotated_image = imagerotate($image, 0, $white); $dest_img = imagecreatetruecolor($new_width,$new_height); $white = imagecopyresampled($dst_img, 255,255,255); ImageFilledRectangle($dst_img,0,0,$new_width,$new_height,$white); // Make the background white 0,0 is the top left corner for first pixel positioning $x_dest_offset = 0; $y_dest_offset = 0; $x_src_offset = 0; $y_src_offset = 0; // These position the original image within the new image // ImageCopyResampled($dst_img,$rotated_image,$x_dest_offset,$y_dest_offset,$x_src_offset,$y_src_offset,$new_width,$new_height,$orig_width,$orig_height); // See php.net for explanations of these functions fully, but hopefully the variable names make sense // /* STEP 6: SAVE THE IMAGE AND THE UPLOADED IMAGE */ /************************************************************************/ // Save the output from above $quality = 100; // 0 Worse -- 100 Best header("content-type: image/jpeg"); ImageJpeg($dst_img,$destfileName,$quality); imagedestroy($image); imagedestroy($rotated_image); if(!(move_uploaded_file($_FILES['form_upload_field_name']['tmp_name'], $image_destination))) { // die("Cannot upload picture file. Story was not added to the database!"); // Use this to cancel all form submission // echo 'Image note saved successfully, please access your profile and try again.'; // Or tell the user to resubmit another image // } } // Continue processing form // ?>