 |
|
 |
|
Next: .asp or perl+LAMP for multitasking ?
|
| Author |
Message |
External

Since: Sep 14, 2004 Posts: 1119
|
(Msg. 1) Posted: Sun Aug 22, 2004 11:41 pm
Post subject: Resize image with PHP Archived from groups: alt>www>webmaster (more info?)
|
|
|
One for Grey, perhaps? The scenario is that I have an image that I have
uploaded (via a form) that is now in a temp directory somewhere on the
server, and I want to use PHP to resize the image to specific dimensions.
Scripts that I have found that do this sort of thing seem to keep the
aspect ratio of the original image, but I don't want to do that - say the
original image was 210x200, I want to resize it to 150x150.
What functions do I need to be using, and is there a really quick and
dirty way of doing it in a few lines of code?
GD is installed on the server, btw, so any solution utilising only that
library would be nice
--
Dylan Parry
http://webpageworkshop.co.uk - FREE Web tutorials and references
'I am a Bear of Very Little Brain, and long words bother me.' -- A A Milne >> Stay informed about: Resize image with PHP |
|
| Back to top |
|
 |  |
External

Since: Aug 02, 2004 Posts: 145
|
(Msg. 2) Posted: Mon Aug 23, 2004 12:17 am
Post subject: Re: Resize image with PHP [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Dylan Parry wrote:
> What functions do I need to be using, and is there a really quick and
> dirty way of doing it in a few lines of code?
>
> GD is installed on the server, btw, so any solution utilising only that
> library would be nice
Assuming ImageMagick is also installed:
<?php
system("convert --resize 150x150 /tmp/image.png /tmp/resized.png");
?>
--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ <a style='text-decoration: underline;' href="http://tobyinkster.co.uk/contact" target="_blank">http://tobyinkster.co.uk/contact</a><!-- ~MESSAGE_AFTER~ --> >> Stay informed about: Resize image with PHP |
|
| Back to top |
|
 |  |
External

Since: Apr 07, 2004 Posts: 80
|
(Msg. 3) Posted: Mon Aug 23, 2004 12:17 am
Post subject: Re: Resize image with PHP [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
"Toby Inkster" <usenet200408.RemoveThis@tobyinkster.co.uk> wrote in message
news:pan.2004.08.22.20.17.34.929835@tobyinkster.co.uk...
> Dylan Parry wrote:
>
> > What functions do I need to be using, and is there a really quick and
> > dirty way of doing it in a few lines of code?
> >
> > GD is installed on the server, btw, so any solution utilising only that
> > library would be nice
>
> Assuming ImageMagick is also installed:
>
> <?php
> system("convert --resize 150x150 /tmp/image.png /tmp/resized.png");
> ?>
>
What Toby said.
IIRC, this preserves the image proportion, too.
So if you upload a rectangular image, then it won't make it all screwy by
converting it to a square.
-Karl<!-- ~MESSAGE_AFTER~ --> >> Stay informed about: Resize image with PHP |
|
| Back to top |
|
 |  |
External

Since: Sep 14, 2004 Posts: 1119
|
(Msg. 4) Posted: Mon Aug 23, 2004 12:34 am
Post subject: Re: Resize image with PHP [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Spake Toby Inkster unto thee:
> Assuming ImageMagick is also installed:
That's something I am not sure of, so it isn't really safe for me to
assume that
> <?php
> system("convert --resize 150x150 /tmp/image.png /tmp/resized.png");
> ?>
Nice and easy, that's what I like, but as I said, I can't assume that
ImageMagick is on the server.
--
Dylan Parry
<a style='text-decoration: underline;' href="http://webpageworkshop.co.uk" target="_blank">http://webpageworkshop.co.uk</a> - FREE Web tutorials and references
Listening to: Howard Shore - At The Sign Of The Prancing Pony<!-- ~MESSAGE_AFTER~ --> >> Stay informed about: Resize image with PHP |
|
| Back to top |
|
 |  |
External

Since: Sep 14, 2004 Posts: 1119
|
(Msg. 5) Posted: Mon Aug 23, 2004 12:44 am
Post subject: Re: Resize image with PHP [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Spake Karl Groves unto thee:
> IIRC, this preserves the image proportion, too.
> So if you upload a rectangular image, then it won't make it all screwy by
> converting it to a square.
That's something I specifically want though! Yes, it might sound strange,
but I have images that *require* changing from rectangles to squares!
--
Dylan Parry
<a style='text-decoration: underline;' href="http://webpageworkshop.co.uk" target="_blank">http://webpageworkshop.co.uk</a> - FREE Web tutorials and references
Listening to: Howard Shore - Many Meetings<!-- ~MESSAGE_AFTER~ --> >> Stay informed about: Resize image with PHP |
|
| Back to top |
|
 |  |
External

Since: Apr 07, 2004 Posts: 80
|
(Msg. 6) Posted: Mon Aug 23, 2004 12:44 am
Post subject: Re: Resize image with PHP [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
"Dylan Parry" <usenet.RemoveThis@dylanparry.com> wrote in message
news:pan.2004.08.22.20.44.03.317028@dylanparry.com...
> Spake Karl Groves unto thee:
>
> > IIRC, this preserves the image proportion, too.
> > So if you upload a rectangular image, then it won't make it all screwy
by
> > converting it to a square.
>
> That's something I specifically want though! Yes, it might sound strange,
> but I have images that *require* changing from rectangles to squares!
I don't think it is too strange. The issue with the whole dynamic resize
thing is that the usability of the page is harmed no matter what you do.
If you're not careful, tt is a short trip to "useless" to provide basically
what is a very small version of a large image. In an ideal world, the
thumbnail will still provide context to the subject of the larger image.
This can't really be done dynamically, and whacking out the proportions only
worsens the problem.
If you find a reliable php solution for this, please post back.
-Karl<!-- ~MESSAGE_AFTER~ --> >> Stay informed about: Resize image with PHP |
|
| Back to top |
|
 |  |
External

Since: Sep 14, 2004 Posts: 2384
|
(Msg. 7) Posted: Mon Aug 23, 2004 12:49 am
Post subject: Re: Resize image with PHP [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
|
|
| Back to top |
|
 |  |
External

Since: Sep 14, 2004 Posts: 1119
|
(Msg. 8) Posted: Mon Aug 23, 2004 1:16 am
Post subject: Re: Resize image with PHP [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Spake Karl Groves unto thee:
> I don't think it is too strange. The issue with the whole dynamic resize
> thing is that the usability of the page is harmed no matter what you do.
Well in this case it won't cause any usability problems. The images in
question are album covers, most if which are square to start with, and
some of which are only out by a couple of pixels. I am simply resizing
them all to 150x150 to create a consistent look across the site, and then
creating a smaller (60x60) version of the image basically for use as a
thumbnail of the album in an artist discography page. The image only needs
to be /representative/ of the actual album.
--
Dylan Parry
<a style='text-decoration: underline;' href="http://webpageworkshop.co.uk" target="_blank">http://webpageworkshop.co.uk</a> - FREE Web tutorials and references
Listening to: Howard Shore - The Breaking Of The Fellowship<!-- ~MESSAGE_AFTER~ --> >> Stay informed about: Resize image with PHP |
|
| Back to top |
|
 |  |
External

Since: Aug 02, 2004 Posts: 145
|
(Msg. 9) Posted: Mon Aug 23, 2004 1:45 am
Post subject: Re: Resize image with PHP [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Toby Inkster wrote:
> <?php
> system("convert --resize 150x150 /tmp/image.png /tmp/resized.png");
> ?>
Apologies, make that:
<?php
system("convert --resize 150x150! /tmp/image.png /tmp/resized.png");
?>
You have to use an exclamation mark to break the aspect ratio.
--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ <a style='text-decoration: underline;' href="http://tobyinkster.co.uk/contact" target="_blank">http://tobyinkster.co.uk/contact</a>
Now Playing ~ ./tomoyasu_hotei_-_battle_without_honour_or_humanity.ogg<!-- ~MESSAGE_AFTER~ --> >> Stay informed about: Resize image with PHP |
|
| Back to top |
|
 |  |
External

Since: Apr 13, 2004 Posts: 235
|
(Msg. 10) Posted: Mon Aug 23, 2004 10:55 am
Post subject: Re: Resize image with PHP [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Dylan Parry wrote:
> One for Grey, perhaps? The scenario is that I have an image that I have
> uploaded (via a form) that is now in a temp directory somewhere on the
> server, and I want to use PHP to resize the image to specific dimensions.
>
> Scripts that I have found that do this sort of thing seem to keep the
> aspect ratio of the original image, but I don't want to do that - say the
> original image was 210x200, I want to resize it to 150x150.
>
> What functions do I need to be using, and is there a really quick and
> dirty way of doing it in a few lines of code?
>
> GD is installed on the server, btw, so any solution utilising only that
> library would be nice
From <a style='text-decoration: underline;' href="http://www.php.net/imagecopyresized" target="_blank">http://www.php.net/imagecopyresized</a>
imagecopyresized() copies a rectangular portion of one image to another
image. Dst_im is the destination image, src_im is the source image
identifier. If the source and destination coordinates and width and heights
differ, appropriate stretching or shrinking of the image fragment will be
performed.
See also <a style='text-decoration: underline;' href="http://www.php.net/imagecopyresampled" target="_blank">http://www.php.net/imagecopyresampled</a>
--
Chris Hope - The Electric Toolbox - <a style='text-decoration: underline;' href="http://www.electrictoolbox.com/" target="_blank">http://www.electrictoolbox.com/</a><!-- ~MESSAGE_AFTER~ --> >> Stay informed about: Resize image with PHP |
|
| Back to top |
|
 |  |
External

Since: Sep 14, 2004 Posts: 1119
|
(Msg. 11) Posted: Mon Aug 23, 2004 10:55 am
Post subject: Re: Resize image with PHP [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Spake Chris Hope unto thee:
<font color=purple> > See also <a style='text-decoration: underline;' href="http://www.php.net/imagecopyresampled</font" target="_blank">http://www.php.net/imagecopyresampled</font</a>>
Well I eventually managed to understand the above page, and it looked like
it was going to do exactly what I wanted it to  Anyway, I came up with
the following script, which I adapted from another source:
---- Start ----
<?php
function resize_image($filename, $new_width, $new_height, $aspect_ratio){
$image = getimagesize ($filename);
$img_width = $image[0];
$img_height = $image[1];
if ($image[2] == 1){
$img = @ imagecreatefromgif($filename);
}
elseif ($image[2] == 2) {
$img = @ imagecreatefromjpeg($filename);
}
elseif ($image[2] == 3) {
$img = @ imagecreatefrompng($filename);
}
if ($aspect_ratio == true) {
if ($img_width < $new_width and ($img_height < $new_height )){
$new_width = $img_width;
$new_height = $img_height;
}
else{
$scale = min($new_width/$img_width,$new_height/$img_height);
if ($scale < 1) {
$new_width = floor($scale*$img_width);
$new_height = floor($scale*$img_height);
}
}
}
$newimage = @imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($newimage, $img, 0, 0, 0, 0, $new_width, $new_height, $img_width, $img_height);
return $newimage;
}
?>
---- end ----
It takes four paranmeters, the image file, new max width, new max height
and the final one is whether the aspect ratio should be kept if the
original image has a width or height smaller than the desired one.
It returns an image as a variable, which can be saved using imagejpeg() or
similar and then destroyed using imagedestroy().
It seems to work so far, although it probably does far too much work when
it could be done in less! Anyone who wants to steal it or improve it for
me is more than welcome.
--
Dylan Parry
<a style='text-decoration: underline;' href="http://webpageworkshop.co.uk" target="_blank">http://webpageworkshop.co.uk</a> - FREE Web tutorials and references
Listening to: Howard Shore - A Knife In The Dark<!-- ~MESSAGE_AFTER~ --> >> Stay informed about: Resize image with PHP |
|
| Back to top |
|
 |  |
External

Since: Apr 13, 2004 Posts: 235
|
(Msg. 12) Posted: Mon Aug 23, 2004 12:20 pm
Post subject: Re: Resize image with PHP [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Toby Inkster wrote:
> Dylan Parry wrote:
>
>> What functions do I need to be using, and is there a really quick and
>> dirty way of doing it in a few lines of code?
>>
>> GD is installed on the server, btw, so any solution utilising only that
>> library would be nice
>
> Assuming ImageMagick is also installed:
>
> <?php
> system("convert --resize 150x150 /tmp/image.png /tmp/resized.png");
> ?>
This is what I love about Unix based systems... there's so much useful
stuff. I think I'd heard of 'convert' before and forgotten all about it. I
did a 'whereis convert' and there it is installed already. Going to be very
useful for me for batch conversions of photos.
--
Chris Hope - The Electric Toolbox - <a style='text-decoration: underline;' href="http://www.electrictoolbox.com/" target="_blank">http://www.electrictoolbox.com/</a><!-- ~MESSAGE_AFTER~ --> >> Stay informed about: Resize image with PHP |
|
| Back to top |
|
 |  |
External

Since: Sep 03, 2004 Posts: 184
|
(Msg. 13) Posted: Mon Aug 23, 2004 2:07 pm
Post subject: Re: Resize image with PHP [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
"Dylan Parry" <usenet.RemoveThis@dylanparry.com> wrote in message
news:pan.2004.08.22.19.41.52.631006@dylanparry.com...
> One for Grey, perhaps? The scenario is that I have an image that I have
> uploaded (via a form) that is now in a temp directory somewhere on the
> server, and I want to use PHP to resize the image to specific dimensions.
>
> Scripts that I have found that do this sort of thing seem to keep the
> aspect ratio of the original image, but I don't want to do that - say the
> original image was 210x200, I want to resize it to 150x150.
>
> What functions do I need to be using, and is there a really quick and
> dirty way of doing it in a few lines of code?
>
> GD is installed on the server, btw, so any solution utilising only that
> library would be nice
>
Chances are you have imagmagic? If so, here is a snippet I use regularly
with upload of a full size image and then resize. to thumb. The final size
is hard coded here but can be replaced with variables.
function makeThumb( $scrFile, $dstFile, $dstW=240, $dstH=200 )
{
$im = ImageCreateFromJPEG( $scrFile ); //creates new image
$srcW = ImageSX( $im );
$srcH = ImageSY( $im );
$ni = ImageCreate( $dstW, $dstH );
ImageCopyResampled( $ni, $im, 0, 0, 0, 0, $dstW, $dstH, $srcW, $srcH );
ImageJPEG( $ni, $dstFile );
}<!-- ~MESSAGE_AFTER~ --> >> Stay informed about: Resize image with PHP |
|
| Back to top |
|
 |  |
| Related Topics: | iframe automatic resize - For example: i have define i frame to size 200x250 pixel. i want that i frame automatic resize when i put html page biger than i frame (example 200x500pixel)??? How can i do that??? i accept any sugestion, html code, javascript anything thanks
Image in a <TD> - Hi, I'm sure this must be easy, but how do I get two images inside a single <TD> and force on to the left of the td and 1 to the right of the <TD> So the effect is <TD>Image1 Image2</TD> Thanks for any help zeebop
Floating an image - Folks, How come, when I have a division with an image floated in it the division doesn't resize to accommodate the image? Eg. <div style="border: 1px solid red;"> <img ... style="float: left;" /> </div> ...
Image Problem - Further to my image problem, Can anyone tell me why a very small percentage of people can't view the jpegs on a site. Is there anything wrong with this HTML?: <img src='../adimages/309thumb.jpg' alt='' border=0> I have had about 5 people saying th...
Image effects - Hi all, I would like to find out, how to "cut" out an image into a circle. Then add effects later like shadow embossing etc with Photoshop 7. Also I would like to add graphical frame/borders to some of my pictures. Any ideas? I would like to c... |
|
You can post new topics in this forum You can reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|
|
|
 |
|
|