von PCFreak » Di 19. Jun 2018, 11:47
Hallo Stefan,
wie wäre folgender Fix?
ORIGINAL
Code: Alles auswählen
private static function scaleImg($fn,$maxDim) {
list($width, $height, $type, $attr) = getimagesize($_FILES['imagefile']['tmp_name']);
if ($width > $maxDim || $height > $maxDim) {
$size = getimagesize($fn);
$ratio = $size[0] / $size[1]; // width/height
if ($ratio > 1) {
$width = $maxDim;
$height = $maxDim / $ratio;
} else {
$width = $maxDim * $ratio;
$height = $maxDim;
}
$src = imagecreatefromstring(file_get_contents($fn));
$dst = imagecreatetruecolor($width, $height);
imagealphablending($dst, false);
imagesavealpha($dst, true);
$transparent = imagecolorallocatealpha($dst, 255, 255, 255, 127);
imagefilledrectangle($dst, 0, 0, $width, $height, $transparent);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
imagedestroy($src);
ob_start();
imagepng($dst); // adjust format as needed
$imagedata = ob_get_contents();
ob_end_clean();
imagedestroy($dst);
return $imagedata;
}
}
FIXED ? - Abfrage auf Größe entfernt, Bedingung für ratio <1 eingebaut.
Code: Alles auswählen
private static function scaleImg($fn,$maxDim) {
list($width, $height, $type, $attr) = getimagesize($_FILES['imagefile']['tmp_name']);
$size = getimagesize($fn);
$ratio = $size[0] / $size[1]; // width/height
if ($ratio > 1) {
$width = $maxDim;
$height = $maxDim / $ratio;
}
if ($ratio < 1) {
$width = $maxDim * $ratio;
$height = $maxDim;
}
$src = imagecreatefromstring(file_get_contents($fn));
$dst = imagecreatetruecolor($width, $height);
imagealphablending($dst, false);
imagesavealpha($dst, true);
$transparent = imagecolorallocatealpha($dst, 255, 255, 255, 127);
imagefilledrectangle($dst, 0, 0, $width, $height, $transparent);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
imagedestroy($src);
ob_start();
imagepng($dst); // adjust format as needed
$imagedata = ob_get_contents();
ob_end_clean();
imagedestroy($dst);
return $imagedata;
}
DIFF
Code: Alles auswählen
diff products.php products.org
1311a1312
> if ($width > $maxDim || $height > $maxDim) {
1317,1318c1318
< }
< if ($ratio < 1) {
---
> } else {
1336a1337
>
Hab es momentan nur mit quadratischen Bildern getestet, ggf. muss noch ein wenig dazuprogrammiert werden, wie gesagt, dafür "spreche" ich zu wenig PHP.
Gruß
Peter
Hallo Stefan,
wie wäre folgender Fix?
ORIGINAL
[code]
private static function scaleImg($fn,$maxDim) {
list($width, $height, $type, $attr) = getimagesize($_FILES['imagefile']['tmp_name']);
if ($width > $maxDim || $height > $maxDim) {
$size = getimagesize($fn);
$ratio = $size[0] / $size[1]; // width/height
if ($ratio > 1) {
$width = $maxDim;
$height = $maxDim / $ratio;
} else {
$width = $maxDim * $ratio;
$height = $maxDim;
}
$src = imagecreatefromstring(file_get_contents($fn));
$dst = imagecreatetruecolor($width, $height);
imagealphablending($dst, false);
imagesavealpha($dst, true);
$transparent = imagecolorallocatealpha($dst, 255, 255, 255, 127);
imagefilledrectangle($dst, 0, 0, $width, $height, $transparent);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
imagedestroy($src);
ob_start();
imagepng($dst); // adjust format as needed
$imagedata = ob_get_contents();
ob_end_clean();
imagedestroy($dst);
return $imagedata;
}
}
[/code]
FIXED ? - Abfrage auf Größe entfernt, Bedingung für ratio <1 eingebaut.
[code]
private static function scaleImg($fn,$maxDim) {
list($width, $height, $type, $attr) = getimagesize($_FILES['imagefile']['tmp_name']);
$size = getimagesize($fn);
$ratio = $size[0] / $size[1]; // width/height
if ($ratio > 1) {
$width = $maxDim;
$height = $maxDim / $ratio;
}
if ($ratio < 1) {
$width = $maxDim * $ratio;
$height = $maxDim;
}
$src = imagecreatefromstring(file_get_contents($fn));
$dst = imagecreatetruecolor($width, $height);
imagealphablending($dst, false);
imagesavealpha($dst, true);
$transparent = imagecolorallocatealpha($dst, 255, 255, 255, 127);
imagefilledrectangle($dst, 0, 0, $width, $height, $transparent);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
imagedestroy($src);
ob_start();
imagepng($dst); // adjust format as needed
$imagedata = ob_get_contents();
ob_end_clean();
imagedestroy($dst);
return $imagedata;
}
[/code]
DIFF
[code]
diff products.php products.org
1311a1312
> if ($width > $maxDim || $height > $maxDim) {
1317,1318c1318
< }
< if ($ratio < 1) {
---
> } else {
1336a1337
>
[/code]
Hab es momentan nur mit quadratischen Bildern getestet, ggf. muss noch ein wenig dazuprogrammiert werden, wie gesagt, dafür "spreche" ich zu wenig PHP.
Gruß
Peter