日曜日, 2月 15, 2009

PHP: Resizing image files

The way to resize image files in PHP is straight foward yet not as simple as it should be. Nevertheless, the PHP image library is equipped with image manipulation functions. Here is a function that does the job -- resizing factor should be edited to suit your needs.



function resizePhoto($fileName,$dest)
{
header('Content-type: image/jpeg');

list($width, $height) = getimagesize($fileName);
$ratio=((float)$height)/$width;
$newWidth=90;
$newHeight=(int)($newWidth*$ratio);

$thumb = imagecreatetruecolor($newWidth, $newHeight);
$source = imagecreatefromjpeg($fileName);

imagecopyresized($thumb, $source, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);

imagejpeg($thumb,$dest);
}

PHP: 定数を扱う

プロジェクトごとの定数を扱うクラス Config\Constants の紹介です。 <?php namespace Config; class Constants {     public const DB_USER = "linguist...