Zobraziť náhľad k obrázku

PHP -> Ukážkové kódy v PHP -> Zobraziť náhľad k obrázku

Zobrazí obrázok test.jpg o veľkosti 300 x 200 bez ohľadu na jeho pôvodnú veľkosť:

<?php
$file = 'test.jpg';    // zdrojovy subor
$newwidth = '300';     // nova sirka
$newheight = '200';    // nova vyska

header('Content-type: image/jpeg');
list($width, $height) = getimagesize($file);
$input = imagecreatefromjpeg($file);
$output=ImageCreate($width, $height);
imagecopyresized($output, $input, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

imagejpeg($output);
?>


Zobrazí obrázok o polovičnej veľkosti s pôvodného:
<?php
$filename = 'test.jpg';
$percent = 0.5;

// Content type
header('Content-type: image/jpeg');

// vyrátať nové rozmery
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// načítať obrázok
$thumb = imagecreate($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// zmeniť veľkosť
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// zobraziť obrázok
imagejpeg($thumb);
?>