Resize and create a new image from another existing image at server side


Cakephp function to create new image from another image:

public function _createHeaderImage($file_name, $theme_name , $thumb_file_name ) {
$final_width_of_image = 960;
$path_theme_directory = ‘../../app/webroot/theme_folder’;
$path_to_image_directory = $path_theme_directory.DS.”$theme_name”;
$filenamelower = strtolower($file_name);
$check = null;
if(strstr($filenamelower, ‘.jpg’)) {
$im = imagecreatefromjpeg($path_to_image_directory.DS.$file_name);
$check = ‘jpg’;
}else if (strstr($filenamelower, ‘.gif’)) {
$im = imagecreatefromgif($path_to_image_directory.DS.$file_name);
$check = ‘gif’;
}else if (strstr($filenamelower, ‘.png’)) {
$im = imagecreatefrompng($path_to_image_directory.DS.$file_name);
$check = ‘png’;
}else if(strstr($filenamelower, ‘.jpeg’)) {
$im = imagecreatefromjpeg($path_to_image_directory.DS.$file_name);
$check = ‘jpeg’;
}
$ox = imagesx($im);
$oy = imagesy($im);
$nx = $final_width_of_image;
//$ny = floor($oy * ($final_width_of_image / $ox));
$ny = 180;
$nm = imagecreatetruecolor($nx, $ny);
imagecopyresized($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);
if(($check == ‘jpg’) ||($check == ‘jpeg’) ){
imagejpeg($nm, $path_to_image_directory.DS.$thumb_file_name);
}else if($check == ‘gif’){
imagegif($nm, $path_to_image_directory.DS.$thumb_file_name);
}else if($check == ‘png’){
imagepng($nm, $path_to_image_directory.DS.$thumb_file_name);
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s