Solved

Resize jpeg (better quality)

Posted on 2007-03-30
7
524 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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 New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
$q.all is slower or not  in  Angularjs 2 21
PHP strings vs array errors 13 32
Split wordpress loop 35 56
AJAX, If No returns don't let the AJAX Call run again 2 20
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.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

792 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