Solved

Resize jpeg (better quality)

Posted on 2007-03-30
7
529 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

707 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