Jump to content

Image resize and rename


stylefrogcreative

Recommended Posts

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
Link to comment
Share on other sites

This should get you on your way...
[code]<?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;
}
?>[/code]
Link to comment
Share on other sites

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!
Link to comment
Share on other sites

The function I posted above would be copied into upload_handler.php and would require the image to already be uploaded:
[code]<?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);
?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.