การปรับขนาดภาพ

ตัวอย่างปรับขนาดภาพ (Resize)

ไฟล์ code/image/resize.php

<?php
header('Content-Type: image/jpeg');
function resize_image($file, $w, $h, $crop=FALSE) {
	list($width, $height) = getimagesize($file);
	$r = $width / $height;
	if ($crop) {
		if ($width > $height) {
			$width = ceil($width-($width*abs($r-$w/$h)));
		} else {
			$height = ceil($height-($height*abs($r-$w/$h)));
		}
		$newwidth = $w;
		$newheight = $h;
	} else {
		if ($w/$h > $r) {
			$newwidth = $h*$r;
			$newheight = $h;
		} else {
			$newheight = $w/$r;
			$newwidth = $w;
		}
	}
	$src = imagecreatefromjpeg($file);
	$dst = imagecreatetruecolor($newwidth, $newheight);
	imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
	$textcolor = imagecolorallocate($dst, 255, 255, 255);
	imagestring($dst, 5, 0, 0, "$newwidth X $newheight", $textcolor);
	return $dst;
}
$im = resize_image("https://phpmysql.khonsongkhla.com/code/img.jpg",$_GET['w'],$_GET['w']);
imagejpeg($im, null);
imagedestroy($im);
?>

คำสั่ง html
<img src="/code/php/resize.php?w=200">
ผลลัพธ์

<img src="/code/php/resize.php?w=400">
ผลลัพธ์

<img src="/code/php/resize.php?w=800">
ผลลัพธ์