Tilted Forum Project Discussion Community

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology
New! Use your Facebook, Google, AIM & Yahoo accounts to securely log into this site, click logo to login  
Register Register Blogs Members List Mark Forums Read

Reply
 
LinkBack Thread Tools
Old 05-24-2007, 08:34 AM   #1 (permalink)
Learning Life's Lessons
 
frogza's Avatar
 
Location: Right Here
PHP Image resize function

I have the GD library installed and running. I can't get this script to work, any suggestions? The picture displays, but not at the size I want.

Quote:
<?php

function imageResize($width, $height, $target) {

//the math to resize the picture maintaining ratio

if ($width > $height) {
$percentage = ($target / $width);
} else {
$percentage = ($target / $height);
}

//gets the new value and applies the percentage, then rounds the value
$width = round($width * $percentage);
$height = round($height * $percentage);

//returns the new sizes in html image tag format

return "width=\"$width\" height=\"$height\"";

}

?>

<?php

//get the image size of the picture
$thumbpic = getimagesize("admin/photos/321-30853-Turpin.JPG");

?>


img src="admin/photos/321-30853-Turpin.JPG" <?php imageResize($thumbpic[0], $thumbpic[1],150); ?>>
the image tag here is missing the opening bracket to help it display properly, in my script it is there.
__________________
A lost temper indicates only an untrained mind.
frogza is offline   Reply With Quote
Old 05-24-2007, 09:32 AM   #2 (permalink)
Darth Papa
 
ratbastid's Avatar
 
Location: Yonder
Blog Entries: 2
I don't really understand what this function is supposed to do, but... Try viewing the source. Are your width and height values getting set correctly in the img tag? That's the place to start.
ratbastid is offline   Reply With Quote
Old 05-24-2007, 10:18 AM   #3 (permalink)
Lover - Protector - Teacher
 
Jinn's Avatar
 
Two things:

1) I see the function declaration, but where is the actual call? Do you have a separate block of code which calls it? I don't see you passing a height, width, target to imageResize. You get it with getimagesize, but never pass that value to the function? Also, without a function call, where is that return value going? It's going nowhere at the moment.

It looks like you're trying to accomplish something like:
PHP Code:
<img src="imgfilename" <?php imageResize($height$width$target); ?>>
2) You're not actually resizing the picture with this code. You are in the effect that you're causing the browser to render the picture at a shrunken size, but someone viewing the page still had to download the full size image, and then their browser shrunk it down. Changing HTML height and width tags only changes rendered size, not actual size.
__________________
If you struggle with something your entire life, try harder.
Awareness without action is worthless, and failure is not an accident.

Last edited by Jinn; 05-24-2007 at 10:22 AM..
Jinn is offline   Reply With Quote
Old 05-24-2007, 10:25 AM   #4 (permalink)
Huggles, sir?
 
seretogis's Avatar
 
Location: Seattle
This is some old code I used to generate a thumbnail from an uploaded image, which may help.

For reference, check out the PHP image function documentation.

Code:
$fullsize = $user_id . "_" . time() . "_" . $_FILES['shot']['name'];
$thumb = "thumb_" . $user_id . "_" . time() . "_" . $_FILES['shot']['name'];

move_uploaded_file( $_FILES['shot']['tmp_name'], $shot_directory . "/" . $fullsize );

list( $width, $height, $type, $attr ) = getimagesize( $shot_directory . "/" . $fullsize );

if ( $type == 1 )
    $src = imagecreatefromgif( $shot_directory . "/" . $fullsize );
else if ( $type == 2 )
    $src = imagecreatefromjpeg( $shot_directory . "/" . $fullsize );
else
    return "ERROR: This gallery only supports JPG and GIF file formats";

// creating a thumbnail ----------------------------------------------------------

$multiplier = 160 / $width;

$new_width = 160;
$new_height = round( $height * $multiplier );

if ( $new_height > 160 ) {
    $multiplier = 0;
    $new_height = 0;
    $new_width = 0;

    $multiplier = 160 / $height;

    $new_height = 160;
    $new_width = round( $width * $multiplier );
}

$dest = imagecreatetruecolor( $new_width, $new_height );

imageCopyResampled( $dest, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

imagejpeg( $dest, $shot_directory . "/" . $thumb );
__________________
seretogis - sieg heil
perfect little dream the kind that hurts the most, forgot how it feels well almost
no one to blame always the same, open my eyes wake up in flames

Last edited by seretogis; 05-24-2007 at 10:31 AM..
seretogis is offline   Reply With Quote
Old 05-27-2007, 07:32 AM   #5 (permalink)
Learning Life's Lessons
 
frogza's Avatar
 
Location: Right Here
Thanks for the help
__________________
A lost temper indicates only an untrained mind.
frogza is offline   Reply With Quote
Reply

Bookmarks

Tags
function, image, php, resize

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 05:53 PM.

Contact Us - Tilted Forum Project - Archive - Top

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
All text © 2002-2009 Tilted Forum Project
"Insignia" vBulletin 3.5 - b6gm6n - x7x7x7.com