Solved

Resize jpeg (better quality)

Posted on 2007-03-30
7
526 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

733 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