Bahaïka
Bahaïka

Reputation: 725

Resizing a Jpeg on server side with PHP

I would like to resize images on server side to make thumbnail dynamicaly. I'm using this code :

<?php
Header("Content-type: image/jpeg"); 
$img_src = $_GET['photo']; 

$size = getimagesize($img_src);

$src_w = $size[0];
$src_h = $size[1]; 

$dst_w = 80;
$dst_h = 80;

$test_h = round(($dst_w / $src_w) * $src_h); 
$test_w = round(($dst_h / $src_h) * $src_w); 


if($src_w > $src_h) {
    $x = $test_w;
    $y = $dst_h;
} elseif($src_h > $src_w) {
    $x = $dst_w;
    $y = $test_h;
}

$img_new = imagecreatefromjpeg($img_src); 
$img_mini = imagecreatetruecolor($x, $y); 
imagecopyresampled($img_mini,$img_new,0,0,0,0,$x,$y,$src_w,$src_h); 
imagejpeg($img_mini); 
?>

But it didn't work, and I can't find why. There is no error, juste nothing appeared. Can anybody help me ?

Thanks;

Upvotes: 0

Views: 993

Answers (4)

Ashwini Dhekane
Ashwini Dhekane

Reputation: 2290

If you are getting a blank page, then either your script timed out or php memory limit was exceeded. Use ini_set function to set memory_limit and max_execution_time before starting any gd function.

Image manipulations take time as well as memory. So these configurations are important.

Upvotes: 2

abelito
abelito

Reputation: 1104

If you are not getting an error, I would work with a test image that you know, and work through this line by line, confirming that you are getitng the results you are expecting, untily you find the line(s) that are causing issues. Check the expected return results for those functions and make conditionals that output where this code is getting stuck or where you are not getting the result you want.

Example:

if (isset($img_src)) {
    echo "No image parameter set in the URL";
    return;
}

Upvotes: 0

pho
pho

Reputation: 25489

Check this line:

$size = getimagesize($img_src);

Where have you defined $img_src? I can't see it!

EDIT Use this code to write the file to disk and then redirect to the written file:

<?php

$chemin = $_GET['photo']; 

$size = getimagesize($chemin);


$src_w = $size[0];
$src_h = $size[1]; 

$dst_w = 80;
$dst_h = 80;

$test_h = round(($dst_w / $src_w) * $src_h); 
$test_w = round(($dst_h / $src_h) * $src_w); 


if($src_w > $src_h) {
    $x = $test_w;
    $y = $dst_h;
} elseif($src_h > $src_w) {
    $x = $dst_w;
    $y = $test_h;
}

$img_new = imagecreatefromjpeg($chemin); 
$img_mini = imagecreatetruecolor($x, $y); 

imagecopyresampled($img_mini, $img_new, 0, 0, 0, 0, $x ,$y, $src_w, $src_h); 

$name=rand() * rand() . '.jpg';

imagejpeg($img_mini, $name); 
header("Location: $name");

?>

Upvotes: 0

tvanfosson
tvanfosson

Reputation: 532465

You probably need to translate the url you are getting into an actual file path so that you can open the file. I expect that the functions you are using expect a file path, not a url. If not, they should otherwise you're doing 2 extra requests just to avoid downloading one image and resizing via CSS on the client.

In any event, if you're going to resize to a thumbnail you really should be caching the thumbnail in a thumbs (or other) subdirectory. Then on each request, check if the thumbnail is exists, serving it up directly if so, before creating the thumbnail. Or, if the files aren't dynamically uploaded, pre-create the thumbnails and just reference them from the thumbs directory directly.

Upvotes: 0

Related Questions