I am using the following code in html to call a php file to create a thumbnail and show it on this page
&w=150&h=&00″ alt=”Image” />
the code of miniature.php is the following :
<?php
function redimensionner_image($chemin_image, $largeur_max, $hauteur_max)
{
list($src_w, $src_h) = getimagesize($chemin_image);
$dst_w = $largeur_max;
$dst_h = $hauteur_max;
if($src_w < $dst_w)
$dst_w = $src_w;
// Teste les dimensions tenant dans la zone
$test_h = round(($dst_w / $src_w) * $src_h);
$test_w = round(($dst_h / $src_h) * $src_w);
if(!$dst_h)// Si Height final non précisé (0)
$dst_h = $test_h;
elseif(!$dst_w) // Sinon si Width final non précisé (0)
$dst_w = $test_w;
elseif($test_h>$dst_h) // Sinon teste quel redimensionnement tient dans la zone
$dst_w = $test_w;
else
$dst_h = $test_h;
$array_ext = explode('.', $chemin_image);
$extension = strtolower($array_ext[count($array_ext)-1]);
if($extension == 'jpg' || $extension == 'jpeg')
$img_in = imagecreatefromjpeg($chemin_image);
else if($extension == 'png')
$img_in = imagecreatefrompng($chemin_image);
else if($extension == 'gif')
$img_in = imagecreatefromgif($chemin_image);
else
return false;
$img_out = imagecreatetruecolor($dst_w, $dst_h);
imagecopyresampled($img_out, $img_in, 0, 0, 0, 0, $dst_w, $dst_h, imagesx($img_in), imagesy($img_in));
imagejpeg($img_out);
}
?>
However, Imagecreatefromjpeg returns a black image after resize. Any help please
First you don’t need to explode the file-extension as
getimagesizewill give you the type:Next I don’t follow your size calculations, try to simplify them, f.e.:
Simple rescaling and preserves aspect ratio.
For reference, here is the code I use in my projects:
Of course, above code will not return image data. It just saves the rescaled image to an other file on the server. But if you pass
NULLas$destFilenameparameter, it will output the image data to the output stream:If you’re still getting a black image, I would suggest increasing the PHP memory limit. If you can modify
PHP.INI, adjust thememory_limitsetting. Otherwise use a.htaccessfile with this line, f.e.:php_value memory_limit 64M.