Solved

Resize jpeg (better quality)

Posted on 2007-03-30
7
521 Views
Last Modified: 2013-12-25
I'm trying to resize a jpeg image to create a thumbnail. The code below does that, but the image is blurry. What am I doing wrong? Is there a better way to resize the image (doesn't have to be PHP)?

<?php
//get source image
$img = "my_image.jpg";
$source = imagecreatefromjpeg($img);
list($s_wd, $s_ht) = getimagesize($img);

//create thumbnail
$wd=$s_wd * 0.5; $ht=$s_ht * 0.5;
$thumb = imagecreatetruecolor($wd, $ht);
imagecopyresized($thumb, $source, 0, 0, 0, 0, $wd, $ht, $s_wd, $s_ht);

//send picture
header("Content-type: image/jpeg");
imagejpeg($thumb);
?>
0
Comment
Question by:itcdr
  • 4
  • 3
7 Comments
 
LVL 4

Expert Comment

by:secondv
ID: 18822324
imagejpeg($thumb, '', 100);

bool imagejpeg ( resource $image [, string $filename [, int $quality]] )

:)
0
 
LVL 1

Author Comment

by:itcdr
ID: 18822343
It doesn't look like it made much of a difference. The image is still much worse than the original. Any ideas?
0
 
LVL 4

Accepted Solution

by:
secondv earned 250 total points
ID: 18822352
Try:

imagecopyresampled

instead of imagecopyresized, and see if that makes a difference for you.


<?php

// get source image
$img = "my_image.jpg";

$source = imagecreatefromjpeg($img);
list($s_wd, $s_ht) = getimagesize($img);

// create thumbnail
$wd = $s_wd * 0.5;
$ht = $s_ht * 0.5;

$thumb = imagecreatetruecolor($wd, $ht);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $wd, $ht, $s_wd, $s_ht);

// send picture
header("Content-type: image/jpeg");
imagejpeg($thumb, '', 100);

?>
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 1

Author Comment

by:itcdr
ID: 18822371
Good job! One little change and it went from blurry to perfect.

Can you explain why it worked?
0
 
LVL 4

Expert Comment

by:secondv
ID: 18822378
To quote the php documentation ;)

"imagecopyresampled() copies a rectangular portion of one image to another image, smoothly interpolating pixel values so that, in particular, reducing the size of an image still retains a great deal of clarity."

http://php.net/imagecopyresampled

:)
0
 
LVL 1

Author Comment

by:itcdr
ID: 18822385
Thanks!
0
 
LVL 4

Expert Comment

by:secondv
ID: 18822390
No problem, glad to help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now