Welcome to WebmasterWorld Guest from 54.234.129.215

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP image resize issue

     

saideep2007

7:47 am on Nov 27, 2010 (gmt 0)

5+ Year Member



Hi guys,


i want to create a thumbnail images for some large images using php...
Here is a example of my problem...click on the url


[gsmexclusive.com ]

Now the problem is the image is getting thumbnailed but its been squeezed and looks odd..can someone help on this issue...so that i can create a gud luking thumbnail using php...Below is my code



==================================================================
<?php
$mysock = getimagesize("Scripts/z500.jpg");

//echo $mysock[0]."<br>";
//echo $mysock[1]."<br>";

print_r($mysock);

//$target = "Scripts/z500.jpg";


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

//takes the larger size of the width and height and applies the formula accordingly...this is so this script will work dynamically with any size image

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...this is so you
//can plug this function inside an image tag and just get the

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

}

?>

<table width="100%" border="1" cellspacing="1" cellpadding="2">
<tr>
<td><img src="Scripts/z500.jpg" border="0" /></td>
<td><img src="Scripts/z500.jpg" <?php echo imageResize($mysock[0], $mysock[1], 150); ?></td>
</tr>

</table>


==================================================================


Awaiting for replies

thank you !
saideep.k

bkeep

9:08 am on Nov 27, 2010 (gmt 0)

5+ Year Member



Can you use something like gd to resize the image?

Also you can set only the width and let the browser figure out the height. I would recommend using some gd image resize function this is some code I use


<?php
//================================================================================
// PLEASE DO NOT REMOVE THIS HEADER!
//
// COPYRIGHT NOTICE
// This script is licensed under the GPL
//================================================================================
// This software IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//================================================================================

// Get extension and return it
$file = isset($_GET["pic"])? $_GET["pic"] : '';
$width = isset($_GET["width"])? (int)$_GET["width"] : 350;
$sold = isset($_GET["sold"])? 1 : 0;

if (!empty($file)) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
header("Content-type: image/$ext");
} else {
exit();
}

function openImage($file)
{
// Get extension and return it
$ext = pathinfo($file, PATHINFO_EXTENSION);
switch(strtolower($ext)) {
case 'jpg':
case 'jpeg':
$im = @imagecreatefromjpeg($file);
break;
case 'gif':
$im = @imagecreatefromgif($file);
break;
case 'png':
$im = @imagecreatefrompng($file);
break;
default:
$im = false;
break;
}
return $im;
}

$im = '';
$im = openImage($file);

if (!empty($im)) {
$old_x = imagesx($im);
$old_y = imagesy($im);

$new_w = (int)$width;

if (($new_w <= 0) or ($new_w > $old_x)) {
$new_w = $old_x;
}

$new_h = ($old_x * ($new_w / $old_x));

if ($old_x > $old_y) {
$thumb_w = $new_w;
$thumb_h = $old_y * ($new_h / $old_x);
}
if ($old_x < $old_y) {
$thumb_w = $old_x * ($new_w / $old_y);
$thumb_h = $new_h;
}
if ($old_x == $old_y) {
$thumb_w = $new_w;
$thumb_h = $new_h;
}

$thumb = imagecreatetruecolor($thumb_w,$thumb_h);

if ($sold == 1) {
$watermark_img = openImage('images/watermark.png');
$watermark_width = imagesx($watermark_img);
$watermark_height = imagesy($watermark_img);

$watermark = imagecreatetruecolor($old_x,$old_y);
imagecopyresampled($watermark,$watermark_img,0,0,0,0,$old_x,$old_y,$watermark_width,$watermark_height);
imagedestroy($watermark_img);

imagecopymerge($im, $watermark, 0, 0, 0, 0, $old_x, $old_y, 30);
imagedestroy($watermark);
}

imagecopyresampled($thumb,$im,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);

//choose which image program to use
switch(strtolower($ext)) {
case 'jpg':
case 'jpeg':
imagejpeg($thumb);
break;
case 'gif':
imagegif($thumb);
break;
case 'png':
imagepng($thumb);
break;
default:
exit();
}

imagedestroy($thumb);
imagedestroy($im);
}
?>


Take the above code and copy it to a file then save it as something like image.php

then in your webpage you can setup the image functions like so
<img src="image.php?pic="Scripts/z500.jpg&width=125" alt="" />

you may have to play with the image path a little depending on your exact setup but you get the drift. you can also set sold=1 and create an image overlay to display over your sold or discontinued items if you need to

Matthew1980

10:57 am on Nov 27, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Hi all,

There are some pretty good scripts written for exactly this sort of use out there already, if you want to look at a few and take the best bit's to make something that's just right for you, have a looks on php class repository (google that, sign in - it's free) and you will see lots of things in there that will help you.

I have used it loads of times in the past, and I made myself a pretty good thumbnailer from mixing and matching, then over time you can add/improve it too.




FYI:-

<img src="image.php?pic="Scripts/z500.jpg&width=125" alt="" /> <<--- need an ampersand there for W3C validation

so it should be:-

<img src="image.php?pic="Scripts/z500.jpg&amp;width=125" alt="" />

Hope that makes sense ;-p

Have fun,

Cheers,
MRb

rainborick

8:25 pm on Nov 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And the reason that you should use a thumbnail script (like the one of those posted above) is that when you just set the width and height in the HTML of your pages, the user will still be downloading the full-sized image file which will make your pages load much more slowly. It also wastes your bandwidth. So use one of these scripts instead. Good luck!

saideep2007

5:33 am on Nov 30, 2010 (gmt 0)

5+ Year Member



Hi all,

we'll test and get back to you

thnks for replies
 

Featured Threads

Hot Threads This Week

Hot Threads This Month