Jump to content


Photo

Image resize and rename


  • Please log in to reply
3 replies to this topic

#1 stylefrogcreative

stylefrogcreative
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 06 September 2006 - 06:19 PM

Hi there,

Got a deadline rapidly approaching and still banging my head against the wall - any help would be greatly appreciated!

Basically I'm developing a Flash application which gives the user the option to customise an ecard with a message, company name and company logo. I've got everything working apart from the logo part.

I need them to upload a logo, which is then resized and uniquely renamed, for when I pull it back in Flash.

I need to specify that neither the height or the width is greater than 75 px, and resize if it is - keeping everything in scale.

Here's the code so far:
I generate a unique variable name and use it create a new text file ($session)

$fd = fopen("$session.txt", "w");
fwrite($fd, "name=$name&message=$message&contentLoaded=true");
fclose($fd);

I then write all the text info into this - which all works perfectly.

I need to know how to let the user specify an image to upload, then once submitted I need to resize it so that neither the width nor the height is greater than 75px. We then need to save it as $session.jpg, so it has a unique name.

Please help guys - I'm pulling my hair out here!

Many, many thanks,

Matt

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 06 September 2006 - 07:56 PM

This should get you on your way...
<?php
function thumbnail($image_path,$thumb_path,$image_name) {
    $max_width = 75;
    $max_height = 75;

    $size = getimagesize($image_path.$image_name);
    $width = $size[0];
    $height = $size[1];
	
    if($width != FALSE && $height != FALSE) {
        $x_ratio = $max_width / $width;
        $y_ratio = $max_height / $height;

        if(($width <= $max_width) && ($height <= $max_height)) {
            $disp_width = $width;
            $disp_height = $height;
        } elseif(($x_ratio * $height) < $max_height) {
            $disp_height = ceil($x_ratio * $height);
            $disp_width = $max_width;
        } else {
            $disp_width = ceil($y_ratio * $width);
            $disp_height = $max_height;
        }
    } else {
        return false;
    }
	
    $src_img = imagecreatefromjpeg($image_path.$image_name);
    $dst_img = imagecreatetruecolor($disp_width,$disp_height);

    imagecopyresized($dst_img,$src_img,0,0,0,0,$disp_width,$disp_height,imagesx($src_img),imagesy($src_img));
    imagejpeg($dst_img, $thumb_path.$image_name);
    return true;
}
?>


#3 stylefrogcreative

stylefrogcreative
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 07 September 2006 - 06:24 AM

Wow - that's fantastic - thanks so much!

With regards to renaming the image file - how would I go about saving it with a unique name? I need to name it with the vaue contained with $session, as thats how I'll call it back into Flash.

Also, in the form which submits the image, how would I reference this new code, at the moment I'd be using:

<form method="post" action="upload_handler.php" enctype="multipart/form-data">
<input type="file" name="logo"><br>
<input type="submit" name="submit" value="Submit">
</form>

Thanks again!

#4 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 07 September 2006 - 06:44 AM

The function I posted above would be copied into upload_handler.php and would require the image to already be uploaded:
<?php
//We're assuming that $session already has a value, image.jpg
//Copy the image from it's temp storage into our new temporary location
move_uploaded_file($_FILES['logo']['tmp_name'],"images/logos/temp/".$session);
//Resize and save the image
thumbnail("images/logos/temp","images/logos/resized/",$session);
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users