Forum Moderators: coopster

Message Too Old, No Replies

Resize images in PHP

         

camaroboy

4:56 pm on Oct 3, 2008 (gmt 0)

10+ Year Member



I am trying to write code to limit or resize images posted in my forums because the width of the page is 500 pixels. Currently if someone links to an image larger than that it throws off the whole design template of the site. I am hoping to get the code for the image to be no more than say 500 pixels and then have it auto adjust the height. The code I currently have now (which will resize the large image put only to 25 x 25) is:

<?php
$msg_text=preg_replace("/img /", "img width=25\% height=25\% ", $msg_text);
echo $msg_text; ?>

Any ideas?

grallis

12:00 am on Oct 4, 2008 (gmt 0)

10+ Year Member



Hi camaroboy -

I can see your problem ... I also once had to resort to resizing an image with the dimension attributes of the image tag, and boy it was ugly!

I have an old image resizing script somewhere ... I'll see if I can dig it up for you. In the meantime, I suggest googling "PHP image resizing". You're likely to get tons of results, and I'm sure a few will be fairly helpful. Keep in mind, resizing images with php isnt always that great, especially when dealing with different image types.

daveginorge

10:16 am on Oct 4, 2008 (gmt 0)

10+ Year Member



Hi taken directly from "PHP in a Nutshell"

I'm learning PHP and not so long ago did this example.

<? php
header("content-type: image/png");
$src_img = imagecreatefrompng("sample.png");
$srcsize = getimagesize("sample.png");
$dst_x = $srcsize[0] / 1.5;
$dst_y = $srcsize[1] / 1.5;
$dst_img = imagecreatetruecolor($dst_x, $dst_y);

Imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $dst_x, $dest_y, $srcsize[0], $srcsize[1]);
imagepng($dst_img);
imagedestroy($src_img);
imagedestroy($dest_img);

?>

I'm sure you can test the $srcsize for aspect ratio and script the $dst_size accordingly to keep the aspect ratio for presentation purposes.

Hope this helps
Dave