I created a function which creats thumbnails and constraints the proportion of the image. Sometimes, however, the function does not seem to work i.e. a black image is returned. Thx for your help!
function resizeimg($file, $path, $maxwidth = "200", $maxheight = "200", $newfile, $newpath)
{
if(file_exists($path."/".$file)) {
// Originalbild einlesen
$h_img= imagecreatefromjpeg("$path/$file");
$width= imagesx($h_img);
$height= imagesy($h_img);
// Größe berechnen
if ($width < $height) {
$qutient = $width / $height;
$thb_y = $maxheight ;
$thb_x = $maxwidth * $qutient;
}
elseif ($width > $height) {
$qutient = $height / $width;
$thb_x = $maxwidth;
$thb_y = $maxheight * $qutient;
}
elseif ($width == $height) {
if ($maxwidth < $maxheight) {
$thb_x = $maxwidth;
$thb_y = $maxwidth;
}
elseif ($maxwidth > $maxheight) {
$thb_x = $maxheight;
$thb_y = $maxheight;
}
elseif ($maxwidth == $maxheight) {
$thb_x = $maxwidth;
$thb_y = $maxheight;
}
}
$thb_x = round($thb_x);
$thb_y = round($thb_y);
// Thumbnail im Speicher erzeugen
$h_thb= imagecreate($thb_x, $thb_y);
// Verkleinern
imagecopyresized(
$h_thb,
$h_img,
0,
0,
0,
0,
$thb_x,
$thb_y,
$width,
$height
);
// In File speichern
imagejpeg($h_thb, $newpath."/".$newfile);
// Aufräumen
imagedestroy($h_thb);
imagedestroy($h_img);
// Ausgabe
return "<img src=\"".$newpath."/".$newfile."\">";
}
else return "File does not exist!";
}