1

I have been dealing with very small images and I have tried to discover the best way to increase them. I compared two different sources: imagecopyresampled (PHP: http://www.php.net/manual/en/function.imagecopyresampled.php) versus Scalr (JAVA: How to resize images in a directory?). I am including the codes that I am using below for the sake of completeness, but they are strongly based on other threads or sites referenced above. If someone thinks that I should remove, I will do that! In both sources it seems that the algorithm used to deal with the issue seems to be the same: Bicubic interpolation. However, in the case of the JAVA source, the quality of the resized image is MUCH MUCH better in my implementation (it is not even possible to compare). Am I doing something wrong when I am using the PHP source? If not, does anyone can explain me the difference between them?

JavaCode:

import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import static org.imgscalr.Scalr.*;

public class App2 {

       public static void main(String[] args) throws IOException {
            for (File sourceImageFile : new File("imgs").listFiles()) {
                if (sourceImageFile.getName().endsWith(".jpg"))
                    res(sourceImageFile.getAbsolutePath());
            }
        }


    public static void res(String arg) throws IOException {

        File sourceImageFile = new File(arg);
        BufferedImage img = ImageIO.read(sourceImageFile);

        BufferedImage thumbnail = resize(img, 500);

        thumbnail.createGraphics().drawImage(thumbnail, 0, 0, null);
        ImageIO.write(thumbnail, "jpg", new File("resized/" + sourceImageFile.getName()));
    }
}

PHP code:

<?php
// The file
$filename = 'photos/thePhoto.jpg';

// Set a maximum height and width
$width = 250;
$height = 250;

// Content type
header('Content-Type: image/jpeg');

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
imagejpeg($image_p, null, 100);
?>

Just to be clear, in the JAVA code, I have to keep the images in a folder. In the PHP code, I provide the name of the file. Off course, I compared exactly the same image. Furthermore, both codes are running without any kind of problem.

![PHP versus Java (in this order)]: http://www.facebook.com/media/set/?set=a.122440714606196.1073741825.100005208033163&type=1&l=55d93c4969

Community
  • 1
  • 1
DanielTheRocketMan
  • 3,199
  • 5
  • 36
  • 65
  • Hmm, interesting. There shouldn't be *that* much of a difference at 100% quality. Can you show example images? (Edit: oh, I notice you are *enlarging* images. That's a different story... but I guess it'd be best if you could show some examples) – Pekka Mar 09 '13 at 18:21
  • I updated my question with a public image! When the size of the images increase, the difference is larger! – DanielTheRocketMan Mar 09 '13 at 19:20
  • Re the examples - ah. The Java code is using a strong blur filter to hide the damage done by the enlargement. That isn't readily available in PHP's GD as far as I know. Can you use ImageMagick instead? (either as a standalone executable, or the PHP library) – Pekka Mar 09 '13 at 19:21
  • @Pekka웃, it seems to be a relevant information. I have never used, but I will try to compare with the imageMagik! – DanielTheRocketMan Mar 09 '13 at 19:27

0 Answers0