Den långsamma hastigheten kan både bero på koden och på din servers prestanda. Tänk på att om det är stora bilder så skall först server öppna dom, beräkna storlek, beräkna thumbnail, göra till thumbnail så om du kör en dålig server så kan du förvänta dig dåligt resultat.
Frågan#1
Hej,
jag har denna kod för att förminska bilder i mitt fotogalleri.
<?
$maxheight = 150;
$maxwidth = 150;
$imgquality = 50;
if (!isset($_GET['picture']) || !file_exists($_GET['picture'])) {
exit;
}
$img_x_y_size = getimagesize($_GET['picture']);
switch($img_x_y_size[2]) {
case 1:
header("Content-type: image/gif");
$imgres = ImageCreateFromGif($_GET['picture']);
break;
case 2:
header("Content-type: image/jpeg");
$imgres = ImageCreateFromJpeg($_GET['picture']);
break;
case 3:
header("Content-type: image/png");
$imgres = ImageCreateFromPng($_GET['picture']);
break;
}
$imgwidth = imagesx($imgres);
$imgheight = imagesy($imgres);
$procentwidth = $maxwidth / $imgwidth;
$procentheight = $maxheight / $imgheight;
if ($procentwidth > $procentheight) {
$newwidth = round($imgwidth * $procentwidth);
$newheight = round($imgheight * $procentwidth);
} else {
$newwidth = round($imgwidth * $procentheight);
$newheight = round($imgheight * $procentheight);
}
if ($maxwidth < $img_x_y_size[0]) {
$thumbres = ImageCreateTrueColor($newwidth, $newheight);
} else {
$thumbres = ImageCreateTrueColor($img_x_y_size[0], $img_x_y_size[1]);
}
if ($maxwidth < $img_x_y_size[0]) {
ImageCopyResampled($thumbres, $imgres, 0, 0, 0, 0, $newwidth, $newheight, $imgwidth, $imgheight);
} else {
ImageCopyResampled($thumbres, $imgres, 0, 0, 0, 0, $img_x_y_size[0], $img_x_y_size[1], $img_x_y_size[0], $img_x_y_size[1]);
}
ImageDestroy($imgres);
switch($img_x_y_size[2]) {
case 1:
ImageGif($thumbres, "", $imgquality);
break;
case 2:
ImageJpeg($thumbres, "", $imgquality);
break;
case 3:
ImagePng($thumbres, "", $imgquality);
break;
}
ImageDestroy($thumbres);
?>
Men när jag öppnar bilderna genom koden tar det ca. 1 sekund/bild att ladda, vilket är otroligt långsamt.
Vad kan jag göra för att snabba på detta?
MVH
Jonas
