By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,871 Members | 2,444 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,871 IT Pros & Developers. It's quick & easy.

i need some code for resizing images

P: n/a
Can someone please tell me where I can get some open source code for
resizing images? I know such code has been written a million times and
I don't feel like doing it again from scratch.
Jul 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"lawrence" a écrit le 01/12/2003 :
Can someone please tell me where I can get some open source code for
resizing images? I know such code has been written a million times and
I don't feel like doing it again from scratch.


It's just a function! ImageCopyResized()
Jul 17 '05 #2

P: n/a
Jedi121 <je*********@free.fr.Removethis> wrote in message news:<me********************************@free.fr.R emovethis>...
"lawrence" a écrit le 01/12/2003 :
Can someone please tell me where I can get some open source code for
resizing images? I know such code has been written a million times and
I don't feel like doing it again from scratch.


It's just a function! ImageCopyResized()


Thank you. I'd like code that offers an option between proportional
and non-proportional resizing. I'm sure they have something like that
over at PHP_CLASSES. But some code is good and some code sucks. So I'm
looking for suggestions.
Jul 17 '05 #3

P: n/a
lawrence:
Jedi121 <je*********@free.fr.Removethis> wrote in message
news:<me********************************@free.fr.R emovethis>...
"lawrence" a écrit le 01/12/2003 :
> Can someone please tell me where I can get some open source code for
> resizing images? I know such code has been written a million times and
> I don't feel like doing it again from scratch.


It's just a function! ImageCopyResized()


Thank you. I'd like code that offers an option between proportional
and non-proportional resizing. I'm sure they have something like that
over at PHP_CLASSES. But some code is good and some code sucks. So I'm
looking for suggestions.


Use ImageMagick.

André Næss
Jul 17 '05 #4

P: n/a


It's just a function! ImageCopyResized()


Thank you. I'd like code that offers an option between proportional and
non-proportional resizing. I'm sure they have something like that over
at PHP_CLASSES. But some code is good and some code sucks. So I'm
looking for suggestions.

Just finished writing and testing one!

Example uses of my function below:

# Resize foo.jpg to a jpg with width 400
resizeImage ('foo.jpg', 'jpg', 400);
resizeImage ('foo.jpg', 'jpeg', 400); // will also work

# Resize foo.jpg to a png with height 500
resizeImage ('foo.jpg', 'png', '', 500);

# Resize foo.jpg to a jpg with width 50 x height 500
resizeImage ('foo.jpg', 'png', 50, 500);

# "Resize" foo.jpg to a jpg the same size as the original
resizeImage ('foo.jpg', 'png');
# Licence: GPL
# Author: Martin Lucas-Smith, University of Cambridge

# Function to resize an image; supported input and output formats are: gif, jpg, png
function resizeImage ($sourceFile, $outputFormat = 'jpg', $newWidth = '', $newHeight = '')
{
# Check that the file exists for security reasons
if (!file_exists ($sourceFile)) {echo '<p>Error: the selected file could not be found.</p>'; return false;}

# Obtain the input format by taking the file extension, allowing for .jpeg and .jpg for JPG format
$inputFileExtension = substr ($sourceFile, -4);
if (substr ($sourceFile, -5) == '.jpeg') {$inputFileExtension = '.jpg';}

# Obtain the source image
switch ($inputFileExtension) {

/* # GIF format
case '.gif':
$sourceFile = ImageCreateFromGIF ($sourceFile);
break; */

# JPG format
case '.jpg':
$sourceFile = ImageCreateFromJPEG ($sourceFile);
break;

# PNG format
case '.png':
$sourceFile = ImageCreateFromPNG ($sourceFile);
break;

# If an invalid format has been requested, return false
default:
echo '<p>Error: an unsupported input format was requested.</p>';
return false;
}

# Obtain the height and width
$originalWidth = ImageSx ($sourceFile);
$originalHeight = ImageSy ($sourceFile);

# Ensure that a valid width and height have been entered
if (!is_numeric ($newWidth) && !is_numeric ($newHeight)) {
$newWidth = $originalWidth;
$newHeight = $originalHeight;
}

# Assign the width and height, proportionally if necessary
$newWidth = (is_numeric ($newWidth) ? $newWidth : (($newHeight / $originalHeight) * $originalWidth));
$newHeight = (is_numeric ($newHeight) ? $newHeight : (($newWidth / $originalWidth) * $originalHeight));

# Create the resized image
$handle = ImageCreateTrueColor ($newWidth, $newHeight);
ImageCopyResampled ($handle, $sourceFile, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);

# Send the image
switch ($outputFormat) {

/* # GIF format
case 'gif':
header ("Content-Type: image/gif");
ImageGIF ($handle);
break; */

# JPG format
case 'jpg':
case 'jpeg':
header ("Content-Type: image/jpg");
ImageJPEG ($handle);
break;

# PNG format
case 'png':
header ("Content-Type: image/png");
ImagePNG ($handle);
break;

# If an invalid format has been requested, return false
default:
echo '<p>Error: an unsupported output format was requested.</p>';
return false;
}
}

Martin Lucas-Smith www.geog.cam.ac.uk/~mvl22
www.lucas-smith.co.uk

Senior Computing Technician (Web Technician) we*******@geog.cam.ac.uk
Department of Geography, University of Cambridge (01223 3)33390

& Webmaster, SPRI we*******@spri.cam.ac.uk
Scott Polar Research Institute, University of Cambridge

Jul 17 '05 #5

P: n/a
André Næss wrote:

lawrence:
Jedi121 <je*********@free.fr.Removethis> wrote in message
news:<me********************************@free.fr.R emovethis>...
"lawrence" a écrit le 01/12/2003 :
> Can someone please tell me where I can get some open source code for
> resizing images? I know such code has been written a million times and
> I don't feel like doing it again from scratch.

It's just a function! ImageCopyResized()


Thank you. I'd like code that offers an option between proportional
and non-proportional resizing. I'm sure they have something like that
over at PHP_CLASSES. But some code is good and some code sucks. So I'm
looking for suggestions.


Use ImageMagick.

André Næss


I second this suggestion.

Shawn
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com
Jul 17 '05 #6

P: n/a


Use ImageMagick.


Is that available for Windows as well?
Martin Lucas-Smith www.geog.cam.ac.uk/~mvl22
www.lucas-smith.co.uk

Jul 17 '05 #7

P: n/a
Martin Lucas-Smith:

> Use ImageMagick.


Is that available for Windows as well?


http://www.imagemagick.org/www/archives.html?

André Næss
Jul 17 '05 #8

P: n/a
André Næss <an*********************@ifi.uio.no> wrote in message news:<bq**********@maud.ifi.uio.no>...
Martin Lucas-Smith:

> Use ImageMagick.


Is that available for Windows as well?


http://www.imagemagick.org/www/archives.html?

André Næss

Thank you. However, I am not able to use ImageMagick. Sorry I didn't
list all requirements in my first post. I need it to be PHP because I
need for it to work on any platform, without people having to worry
about platform issues. ImageMagick apparently has different source
files for the major platforms, and therefore is unusable for my
purposes. But again, thank you for the suggestion.
Jul 17 '05 #9

P: n/a
lawrence:
Thank you. However, I am not able to use ImageMagick. Sorry I didn't
list all requirements in my first post. I need it to be PHP because I
need for it to work on any platform, without people having to worry
about platform issues. ImageMagick apparently has different source
files for the major platforms, and therefore is unusable for my
purposes. But again, thank you for the suggestion.


You will experience platform problems anyway as there is no built-in way to
handle images in PHP. The image functions offered by PHP require the GD
library, so people will have to build this library as well. In fact they
have to compile PHP with explicit support for GD. ImageMagick on the other
hand is stand-alone software.

André Næss
Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.