homepage Welcome to WebmasterWorld Guest from 23.21.9.44
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

    
Image resizing with PHP
need some pointers
HelenDev




msg:3798240
 2:31 pm on Dec 2, 2008 (gmt 0)

I have a form which uploads some info and an image into a database.

I would like to create a resized version of said image, and either save it somewhere or simply display it on the page.

I have been having a look at some code but have not been getting anywhere with trying to piece something together.

I have used getimagesize() successfully to get info about the image and have tried using imagecopyresized() to display a resized copy, but with no luck.

Here is some sample code I was trying to adapt


// File and new size
$filename = '../path/to/my/file.jpg;
$percent = 0.5;

// Content type
header('Content-type: image/jpeg');

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
imagejpeg($thumb);

I think part of the problem is the header('Content-type: image/jpeg'); bit, if I remove it I get a load of weird code on the page, and if I leave it in I just get a page which echoes its own url. Help!

 

jatar_k




msg:3798461
 7:47 pm on Dec 2, 2008 (gmt 0)

have you tried testing the return values of the various functions you are using to see what failed?

penders




msg:3798727
 12:36 am on Dec 3, 2008 (gmt 0)

Make sure you have no output before or after your imagejpeg(). Blank lines before opening/closing <?php tags, anything. Also try
exit; immediately after your imagejpeg().
acemaster




msg:3799420
 8:24 pm on Dec 3, 2008 (gmt 0)

Do you need this as a seperate script altogether, or can you do this in a main script and resize with the HTML <img> tag? Something like:

<?
// Image file
$filename = "path/to/file.jpg";

// Set width and height you want
$desiredWidth = ###px;
$desiredHeight = ###px;

// Image details
list ($width, $height) - getimagesize($filename);

// Resizes height/width proportionally to desired width
if ($width > $desiredWidth){
$height = ($height/$width)*$desiredWidth;
$width = $desiredWidth;
}

// Resizes height/width proportionally to desired width
if ($height > $desiredHeight){
$width = ($width/$height)*$desiredHeight;
$height = $desiredHeight;
}

// Display image on HTML page
echo "<img height='$height' width='$width' src='$filename' />";
?>

penders




msg:3799538
 11:22 pm on Dec 3, 2008 (gmt 0)

or can you do this in a main script and resize with the HTML <img> tag?

This is very different to the OPs original query of using the GD library to actually create a new resized image and send this to the client. To be honest, using PHP to create a new image of the correct size is by far the better solution (over letting HTML do the resizing), particularly if this is then saved/cached server side.

$percent = 0.5;

With a linear scale factor of 0.5 you are potentially looking at a resized image which is 1/4 of its original size. To send the original image and let HTML do the work is an unnecessary bandwidth hog and the quality will be poor (in most browsers).

Using PHP to do the work will be quicker, better quality and will use less bandwidth - everyone's a winner.

acemaster




msg:3799735
 5:05 am on Dec 4, 2008 (gmt 0)

To be honest, using PHP to create a new image of the correct size is by far the better solution (over letting HTML do the resizing), particularly if this is then saved/cached server side.

Interesting. How about when calling an image from a non-local site, would it still be better to resize/recreate with php?

wsmeyer




msg:3799795
 7:33 am on Dec 4, 2008 (gmt 0)

Check this line:

$filename = '../path/to/my/file.jpg;

you need a closing '

$filename = '../path/to/my/file.jpg';

William.

HelenDev




msg:3802596
 3:34 pm on Dec 8, 2008 (gmt 0)

Thanks for your replies guys. Getting rid of all the output before the header and using exit; did the trick :)

As I delve further into this project however, I think I really need to save a copy of the resized image rather than linking to it - how can I go about this?

HelenDev




msg:3831173
 12:02 pm on Jan 21, 2009 (gmt 0)

For anyone who was interested in this topic, here is my end solution.

Thanks again for your help, it was nearly there, just needed to save the file rather than output it, and I swapped imagecopyresized for imagecopyresampled, which produces a much better looking image:

<?php

// File and new size
$filename = 'pathtofile/testpic.jpg';

//echo($filename);

// Get new sizes
list($width, $height) = getimagesize($filename);

//figure out height ratios

$newwidth = 200;
$newheight = ($newwidth/$width) * $height;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Save the image as 'mynewpic.jpg'
imagejpeg($thumb, 'pathtofile/mynewpic.jpg',85);

// Free up memory
imagedestroy($thumb);
?>

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