473,796 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[Request] I am looking for a high-quality JPG resize/optimize function

Hi, I am looking for a complete PHP function that will resize &
optimize any JPG image with very good end results. My server is
running PHP 4.3.8.

So, for instance a 1600x1200 JPG at ../somewhere/picture.jpg

could be read by the function and save where I specify like
.../somewhere/pictureResized. jpg

.....what is most important for me besides a good end result is the
function should calculate which side of the image is larger, and based
on a maximum value I preset (like 800), adjust the image proportionally
to 800x600 (for example).

Thanks so much for anyone's assistance with this!

BEST REGARDS -

ALEX

Jul 17 '05 #1
4 3012
In comp.lang.php Ra*********@hot mail.com wrote:
So, for instance a 1600x1200 JPG at ../somewhere/picture.jpg

could be read by the function and save where I specify like
../somewhere/pictureResized. jpg
imagejpeg($newi mage,$pathtores izedpicture)
....what is most important for me besides a good end result is the
function should calculate which side of the image is larger, and based
on a maximum value I preset (like 800), adjust the image proportionally
to 800x600 (for example).


Almost trivial, see the function resize() in
http://tmp.tryba.nl/resize/img.phps

used to serve reduced images from raw jpegs created by a camera
(http://jamie.tryba.nl/slideshow.php)

Jul 17 '05 #2
Could you pls help me with actual implementation of this, so if I have
a 1600x1200 picture, how to tell it where it is, and where to save a
resized version on the server:

<?php

function resize($file,$n x,$ny)
{
$image=imagecre atefromjpeg($fi le);

$x=imagesx($ima ge);
$y=imagesy($ima ge);
$r=$x/$y;

$mr=$nx/$ny;

if($r>$mr)
{
$f=$x/$nx;
}
else
{
$f=$y/$ny;
}

$nx=round($x/$f);
$ny=round($y/$f);

$newimage=image createtruecolor ($nx,$ny);
imagecopyresamp led($newimage,$ image,0,0,0,0,$ nx,$ny,$x,$y);

return $newimage;
}

?>

Jul 17 '05 #3
http://pear.php.net/package/Image_Transform

Regards,

Peter

<Ra*********@ho tmail.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Hi, I am looking for a complete PHP function that will resize &
optimize any JPG image with very good end results. My server is
running PHP 4.3.8.

So, for instance a 1600x1200 JPG at ../somewhere/picture.jpg

could be read by the function and save where I specify like
../somewhere/pictureResized. jpg

....what is most important for me besides a good end result is the
function should calculate which side of the image is larger, and based
on a maximum value I preset (like 800), adjust the image proportionally
to 800x600 (for example).

Thanks so much for anyone's assistance with this!

BEST REGARDS -

ALEX

Jul 17 '05 #4
ALEX,

There are many different ways of scaling an image.

depending on the amount of detail and amount of colors in the original, you
should use different methods. It is sometimes better not to scale an image
at once, but in several steps instead, where each step isn't bigger than
49% scaleing, to preserve quality.

one simple php-script should be like this:
<?php
// The file
$filename = 'test.jpg';
$outputfilename = "/somewhere/picture.jpg";

// Set a maximum height and width
$width = 200;
$height = 200;

// Get new dimensions
list($width_ori g, $height_orig) = getimagesize($f ilename);

if ($width && ($width_orig < $height_orig)) {
$width = ($height / $height_orig) * $width_orig;
} else {
$height = ($width / $width_orig) * $height_orig;
}

// Resample
$image_p = imagecreatetrue color($width, $height);
$image = imagecreatefrom jpeg($filename) ;
imagecopyresamp led($image_p, $image, 0, 0, 0, 0, $width, $height,
$width_orig, $height_orig);

// Output
imagejpeg($imag e_p, $outputfilename , 100);
?>

play with it..

more you can find on:
http://nl.php.net/manual/en/function...yresampled.php

greetings Caspar.


Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5754
by: Sure | last post by:
Hello All, I want to update a form using the LWP & HTTP method. It was working fine when I am updating the values like this $ua = LWP::UserAgent->new; $url ='http://xxx.be/cgi-bin/viewauth/Tracking/TestProjectAgainInitialDevStory#edittable2'; use HTTP::Request::Common; my $res = $ua->request(POST $url, Content_Type =>'form-data',
3
2284
by: mrhicks | last post by:
Hello all, I have a question regarding efficeny and how to find the best approach when trying to find flag with in a structure of bit fields. I have several structures which look similar to typedef unsigned long int uint32; /* 32 bits */ // Up to 36 request flags, so this will take up to 3
136
5551
by: Merrill & Michele | last post by:
A derangement is a mapping of a set onto itself leaving no element fixed. I realized that that was what I was programming when I was asked to randomly determine who buys presents for whom this year in my wife's family. Obviously, one does not buy for himself. The code is followed by some questions. #include <stdio.h> #include <stdlib.h> #include <time.h>
7
4148
by: Shapiro | last post by:
I have a scenario where I log a resquest to a database table and update the request with a corresponding response including the response time. I am using an HttpModule to do this. My challenge is how to corelate the response to a corresponding request. I am looking for a sure proof threadsafe way to corelate a response to a coresponding request message. Two things that concerns me:-
1
11456
by: Alex Nitulescu | last post by:
I have the following very simple colde (while learning about cookies and session state): Private Sub cmdAddCookie_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddCookie.Click Dim strCookieName As String Dim objRandom As New Random() strCookieName = "MyCookie" & objRandom.Next(Integer.MaxValue).ToString
3
2812
by: ExclusiveResorts | last post by:
Can the CallContext be used reliably for storing request specific data? We are developing an application library that uses the CallContext to keep an IdentityMap (hashtable of business objects that have been loaded from the DB) and a collection of business objects that have been modified during the current request. These object maps need to be globally visible throughout a request and expire at the end of it. The maps are accessed using...
3
1358
by: scorpion53061 | last post by:
Hi, I am looking for a code sample that communicates from a client to a remote Windows Service on another computer over the internet, that is the Windows Service performing instructions the client sends. That being said, I also would like feedback on this. Currently in my Windows applications, I do a lot of communication with my servers at work from customers to place orders, check stock status etc. The production server is a AIX...
1
6456
by: Pavils Jurjans | last post by:
Hello, It is possible to set a high value in the web.config file, to prevent request timeout for a very complex pages that take long time to generate: <system.web> <httpRuntime executionTimeout="300"/> But, I do not like to keep such a high value as a default. I better keep there a low value, and in case I need to generate complex report, I'd change
18
5449
by: Thomas Lunsford | last post by:
I have inherited a set of asp pages that I now need to augment. In order to minimize changes to production code, I would like to make a "call" to an asp page from a new asp page. Existing code is using many Request.Form variables, and I would very much prefer not to change this code. The new page will retrieve data that I would like to fill into Request.Form variables that are used in the old code. So, is it possible for me to set...
2
1713
by: Hiten | last post by:
Our application is created in C#, ASP.Net 2.0, SQL 2005; the session is managed in SQL server with ASPState. We often file below exception Error Details : Exception of type 'System.Web.HttpException' was thrown. Source : System.Web Stack Information : at System.Web.HttpAsyncResult.End() at
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9535
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10200
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6800
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5453
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4127
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.