Solved

Resize jpeg (better quality)

Posted on 2007-03-30
7
519 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

706 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

22 Experts available now in Live!

Get 1:1 Help Now