Вот что я использую атм.Не стесняйтесь извлекать вам нужно:
Usage:
<CLASS>::scale_image($dir.$name, 1024, 768, $dir.'thumb_'.$name);
/**
* Most simple way to get File Extension
* @param string $path Local path to File
* @return string Extension in lower case
*/
public static function extension($path){
return strtolower(pathinfo($path, PATHINFO_EXTENSION));
}
/**
* Rename Filename for more secure usage
* @param string $name filename
* @return string more secure filename
*/
public static function secure_name($name){
return urlencode(preg_replace('/[^a-z0-9 \-_\.]/i', '_', strtolower($name)));
}
/**
* Scale Image without ratio changes
* @param int $sw source width of orig image
* @param int $sh source height of orig image
* @param int $tw max target width
* @param int $th max target height
* @return array list($width, $height)
*/
public static function scale($sw, $sh, $tw, $th){
if ($sw > $tw && $sw/$tw > $sh/$th) {
$tw = $sw * ($tw / $sw);
$th = $sh * ($tw / $sw);
}else if ($sh > $th) {
$tw = $sw * ($th / $sh);
$th = $sh * ($th / $sh);
}else{
$th = $sh;
$tw = $sw;
}
return array(round($tw), round($th));
}
/**
* Scale Image
* @param string $sPath
* @param int $width max width
* @param int $height max height
* @param string $tPath Optional Path for Thumbnail
* @param int $thumbScale Scale ratio for optional Thumbnail (Width and Height / $thumbScale)
* @return void
*/
public static function scale_image($sPath, $width, $height, $tPath = NULL, $thumbScale = 10){
if(!function_exists('imagecreatetruecolor')){
return;
}
$ext = strtolower(self::extension($sPath));
if($ext == 'jpg' or $ext == 'jpeg'){
$src = imagecreatefromjpeg($sPath);
}elseif($ext == 'png'){
$src = imagecreatefrompng($sPath);
}elseif($ext == 'gif'){
$src = imagecreatefromgif($sPath);
}else{
return;
}
list($sw, $sh) = getimagesize($sPath);
list($tw, $th) = File::scale($sw, $sh, $width, $height);
$trg = imagecreatetruecolor($tw, $th);
imagecopyresampled($trg, $src, 0, 0, 0, 0, $tw, $th, $sw, $sh);
imagejpeg($trg, $sPath, 90);
if($tPath){
$tw = (int)$tw / $thumbScale;
$th = (int)$th / $thumbScale;
$trg = imagecreatetruecolor($tw, $th);
imagecopyresampled($trg, $src, 0, 0, 0, 0, $tw, $th, $sw, $sh);
imagejpeg($trg, $tPath, 90);
}
}