PHP 載入圖檔

可用於載入內部非web資料夾圖檔,或外部http圖檔

$source_img = '/home/test/test.jpg'; 
//$source_img = 'https://test.jpg';
@$im = imagecreatefromjpeg($source_img);


if(!$im){ // 載入失敗顯示錯誤圖
	$w = 200;
	$h = 200;
	$fontsize = 5; // 1,2,3,4,5 max 5
	$errorstr = 'Image error';
	$im  = imagecreatetruecolor($w, $h); 
	$bgcolor = imagecolorallocate($im, 255, 255, 0);
	$textcolor  = imagecolorallocate($im, 0, 255, 255);

	imagefilledrectangle($im, 0, 0, $w, $h, $bgcolor); // 填滿背景
	$fw = imagefontwidth($fontsize);     // width of a character
	$fh = imagefontheight($fontsize);     // height of a character
	$l = strlen($errorstr);          // number of characters
	$tw = $l * $fw;              // text width
	$th = 1 * $fw;              // text height

	$xpos = ($w - $tw)/2;
	$ypos = ($h - $th)/2;

	imagestring($im, $fontsize, $xpos, $ypos, $errorstr , $textcolor);
}

header("Content-type: image/jpeg");
imagejpeg($im);
imagedestroy($im);
exit;