homepage Welcome to WebmasterWorld Guest from 54.161.246.212
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Resizing an image - question on if/then
naitsirhc26

5+ Year Member



 
Msg#: 3666412 posted 6:54 am on Jun 4, 2008 (gmt 0)

Okay - so I have a simple image uploader - that makes a thumbnail - and resizes the original image if it is too large. My problem is that even though the program works - and resizes the original image - I don't want the image to be resized if it is SMALLER than the max width/height of 400 pixels. How would I write an if/then statement in there that would only resize the original if it was LARGER than 400/400? Because now - it resizes even tiny images to 400/400 pixels, and they come out really blurry when they are displayed. Any thoughts?


// upload the file if it exists
$file= uploadImage($_POST["uploadform"]);
// Create a Thumbnail if an image exists
if ($file != "no file")
{
$date = date("YmdHis");
$imgArr = split('[/]', $file);
$imgNameOnly = $imgArr[sizeof($imgArr)-1];
$folderPath = "";
for ($i=0; $i<sizeof($imgArr)-1; $i++)
$folderPath .= $imgArr[$i] . "/" ;

$photoPathName = $folderPath . $date . "_" . $imgNameOnly;
$thumbNailPathName = $folderPath . "thumb_" . $date . "_" . $imgNameOnly;
$imgType = getImgType($imgNameOnly);
// Create a resized image of the orig. a mx of 400 pixels
[b]$photo=new Thumbnail(400,400);[/b]
// Load an image into a string (this could be from a database)
$image=file_get_contents($file);
// Load the image data
$photo->loadData($image,$imgType);
// Build the thumbnail and store as a file
$photo->buildThumb($photoPathName);

chmod("$photoPathName", 0666);

// Instantiate the thumbnail
$tn=new Thumbnail(100,100);
// Load an image into a string (this could be from a database)
$image=file_get_contents($file);
// Load the image data
$tn->loadData($image,$imgType);
// Build the thumbnail and store as a file
$tn->buildThumb($thumbNailPathName);
// delete the source file that is potentially large
unlink($file);

chmod("$thumbNailPathName", 0666);
}

 

PHP_Chimp

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3666412 posted 7:03 am on Jun 4, 2008 (gmt 0)

Could you use getimagesize [uk2.php.net] to give you the dimensions?

Then you could use if/else to decide if you want to resize.

naitsirhc26

5+ Year Member



 
Msg#: 3666412 posted 7:29 am on Jun 4, 2008 (gmt 0)

Would the following work?


// upload the file if it exists
$file= uploadImage($_POST["uploadform"]);
// Create a Thumbnail if an image exists
if ($file != "no file")
{
$date = date("YmdHis");
$imgArr = split('[/]', $file);
$imgNameOnly = $imgArr[sizeof($imgArr)-1];
$folderPath = "";
for ($i=0; $i<sizeof($imgArr)-1; $i++)
$folderPath .= $imgArr[$i] . "/" ;

$photoPathName = $folderPath . $date . "_" . $imgNameOnly;
$thumbNailPathName = $folderPath . "thumb_" . $date . "_" . $imgNameOnly;
$imgType = getImgType($imgNameOnly);
// Create a resized image of the orig. a mx of 400 pixels

$foo = getimagesize("$photo");
$width=$foo[0];

if($width < '400'){
// Load an image into a string (this could be from a database)
$image=file_get_contents($file);
// Load the image data
$photo->loadData($image,$imgType);
// Build the thumbnail and store as a file
$photo->buildThumb($photoPathName);

chmod("$photoPathName", 0666);

// Instantiate the thumbnail
$tn=new Thumbnail(100,100);
// Load an image into a string (this could be from a database)
$image=file_get_contents($file);
// Load the image data
$tn->loadData($image,$imgType);
// Build the thumbnail and store as a file
$tn->buildThumb($thumbNailPathName);
// delete the source file that is potentially large
unlink($file);

chmod("$thumbNailPathName", 0666);
} else {

$photo=new Thumbnail(400,400);
// Load an image into a string (this could be from a database)
$image=file_get_contents($file);
// Load the image data
$photo->loadData($image,$imgType);
// Build the thumbnail and store as a file
$photo->buildThumb($photoPathName);

chmod("$photoPathName", 0666);

// Instantiate the thumbnail
$tn=new Thumbnail(100,100);
// Load an image into a string (this could be from a database)
$image=file_get_contents($file);
// Load the image data
$tn->loadData($image,$imgType);
// Build the thumbnail and store as a file
$tn->buildThumb($thumbNailPathName);
// delete the source file that is potentially large
unlink($file);

chmod("$thumbNailPathName", 0666);
}
\\above is my edit
}

naitsirhc26

5+ Year Member



 
Msg#: 3666412 posted 8:13 am on Jun 4, 2008 (gmt 0)

$foo = getimagesize("urlgoeshere");
$picwidth=$foo[0];
$picheight=$foo[1];

echo $picheight;

I'm trying to test out the getimagesize function - and learn how it works. Afterwords - I'll implement it into the code I have already posted in other posts. I've tried the above code - with the url of an image - and it seems to work with outputting the image's height. So how would I implement those ideals into the code that I've posted in the first post? Maybe an if/else statement comparing a smaller then satement of 400 pixels? Any thoughts on how I can put this together? Thanks!

PHP_Chimp

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3666412 posted 10:46 am on Jun 4, 2008 (gmt 0)

$foo = getimagesize("urlgoeshere");
$picwidth=$foo[0];
$picheight=$foo[1];

Will give you the width and height. So you would need to do something like -

if (($picwidth > 400) && ($picheight > 400)) {
// resize
}
else {
// image is smaller than 400 so no point in resizing.
}

You seem to be getting along fine with the getimagesize :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved