473,396 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

i need some code for resizing images

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
9 5032
"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
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
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


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
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


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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Alex Hopson | last post by:
I'm using the code below to loop through some images and resize each image twice, once to create a thumbnail and once to create a small image. The page stops loading around the 38th image out of...
9
by: Bryan R. Meyer | last post by:
Hello Everyone, The problem of browser resizing has become an issue for me. While redesigning my webpage, I set the left and right margins to be auto so that my content would be centered. ...
13
by: Jon Yeager | last post by:
I need to display a bunch of pictures that are all of various dimensions into a fixed dimension space, like MSN Messenger does with its user photos in the chat windows. Forcing image dimensions...
3
by: pablo | last post by:
Dear NG'ers, I want to keep my images at a certain position that should change when the text is enlarged or shrunk with the button on the toolbar? What event is triggered when resizing text in...
6
by: Robert J. O'Hara | last post by:
I'm one of those people who practices what some consider "dull" and others consider "elegantly conservative" page design. I appreciate good traditional typography and standards-compliant liquid...
5
by: Jim | last post by:
I've heard that resizing images through PHP (either GD2 or ImageMagick) is a processor intensive exercise. I'm setting up a site where users will be uploading up to 10 images along with the details...
4
by: funcSter | last post by:
Hey, got a prob which is driving me nuts! I'm trying to resize the resolution of an image as well as it's pyhsical byte size. I've got: byte bytImage = null; System.Drawing.Image imgImage =...
1
by: Geuis | last post by:
Hi, new to the group. I use the following code to resize images my users upload. They're either blurry or they're jaggy. Can someone recommend modifications or different code that will resize...
6
by: tomasio | last post by:
Dear NG, years have passed and I am still more designer than programmer. I build a new version of my website which has a few nasty bugs, especially on my startpage: Resizing text brakes the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.