[thelist] Adding to the Login System with Admin Features: Two Questions
Tony Spiro
spirony at gmail.com
Fri Feb 13 17:14:52 CST 2009
Thanks for this. I'm trying to use it, but I don't have gd configured. I
have PHP 5, but when I pull up phpinfo, gd is not configured, please any
help is appreciated.
On Wed, Feb 11, 2009 at 8:48 AM, Hugh Miller <hmiller at cfpress.co.uk> wrote:
> Tony,
>
> You would typically use a the PHP GD functions to do this; It can be
> achieved by a very simple bit of code, but you have to think about what your
> users will submit, portrait, square and landscape images, some widescreen,
> some self cropped first etc etc. This makes it quite a bit more challenging.
>
> Typically thumbnails would be cropped to compensate for this.
>
> Here is what I do:
>
> 1. Check an image was actually sent with data
> 2. Get that image for processing and create a new name (I use a unix
> timestamp plus .jpg to avoid duplication)
> Set the path to full sized images
> Set the path to thumbnails
> 3. Compare the file I received to jpg, gif and png formats (or else you can
> have a situation where they submit a gif and your jpg processor dies.
> 4. Get the original filesize and determine your preferred width and height
> for new images
> 5. Crop and scale the image
> On some scripts at this point I rotate images instead
> 6. Save the output (and original file if needed)
>
> -------
>
> Before you panic, this is achieved with less than you might think because
> php includes all of the functions that you need.
>
> I've attached a text file with an example of the above workflow, it's not a
> tutorial but the comments that are there tell you what happens in each
> block, this has been lifted from a working example I use and so may have
> some quirks in there that are not really suitable for what you are doing
> (for example there is some offsetting being performed on the crop
> functionality to make the top of a portrait image more important than the
> bottom).
>
> Regards,
>
> H
>
> Tony Spiro wrote:
>
>>
>> 2. I am using this fantastic script as well
>> http://dottedi.biz/codesamples/forms/upload/upload_file.php to upload
>> user
>> profile pics and I need to know how to resize the image on upload so a
>> 3000px wide picture is not crammed into a 50px wide space, causing the
>> load
>> time to be slow.
>>
>> Any help is much appreciated!
>> Thanks,
>> Tony
>>
>>
>
> --
>
>
> --
> This message has been scanned for viruses and
> dangerous content by MailScanner, and is
> believed to be clean.
>
>
> <?php
>
> /* STEP 1: Check for an uploaded image */
> /************************************************************************/
>
> if($_FILES['form_upload_field_name']['size'] > 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 //
>
> ?>
> --
>
> * * Please support the community that supports you. * *
> http://evolt.org/help_support_evolt/
>
> For unsubscribe and other options, including the Tip Harvester
> and archives of thelist go to: http://lists.evolt.org
> Workers of the Web, evolt !
>
More information about the thelist
mailing list