php image resize? is there no simple method?

Joined
12 Feb 2006
Posts
17,629
Location
Surrey
really struggling with this, spending far too much time to get absolutely no where with it.

what i want is a simple easy way to get image file, resize it setting the width to a specific size and then having the height shrink proportionally, and then save the image ready for use later on. Does such a thing exists? i have search and searched but every single thing i find doesn't do what i want, creating the thumbnail dynamically. Surely there must be a simple way like with html image tag setting the width of an image, but then saving the image's new width.
 
If you've got ImageMagick in your path it's super-duper easy:

Code:
<?php
`convert -resize 150x150 foo.jpg foo-small.jpg`;
?>

You have to do it all yourself if you're doing it with GD, but it's not a ridiculously hard thing to do--have a bash at working it out yourself.
 
so GD is the way to do it? sorry if it seems a silly question just image part of php is new to me and far far more powerful then i first anticipated so a lot more confusing at first.

just to check then, the most common method i come across is to use imagecopyresampled, is this correct to use? i can get it working, though not with gif which is needed, but it created on the fly and not saved for use later on. If this is what i am after though with some tweaking then i will continue to investigate it. Just seems a much more complicated way to simply resize an image then i'd expect
 
Yeah, imagecopyresampled is the method you'll use to do the actual resizing; the bit you have to do is calculate the width and height that you'll be resizing to. To save the images, pass a filename to imagejpeg/imagepng/whatever you're outputting the final image as.

Use ImageMagick if you can, though, it'll save a lot of headaches (plus you can do tons more cool stuff with it).
 
thanks everyone, especially robmiller for the advice, really helped and i finally got it exactly how i wanted :)

just a quick unrelated question while have this thread, when i insert into fields into the database each record has a unique id which is auto incremented so i don't know what it is, is there a way to say insert this into the database and return with the id of that record?

i know i could do an insert into query and then after a select query but trouble with that is i don't have a unique id to select from the table so would have to use a few fields at least to be safe which is a lot more work then if there is a better way like i have asked above?

thanks again
 
just a quick unrelated question while have this thread, when i insert into fields into the database each record has a unique id which is auto incremented so i don't know what it is, is there a way to say insert this into the database and return with the id of that record?

Either:

http://uk.php.net/mysql_insert_id

...or:

Code:
SELECT LAST_INSERT_ID() FROM foo

...where `foo` is your table.
 
Always use mysql_insert_id() rather than the query, because there's no garuntee that someone else hasn't done another insert in between you doing it and the query.
 
Back
Top Bottom