This is the mail resizing image function which actually resized the image based on provided parameter.
/**
* This will resize the image into specific image sizes.
* @param array $data: 'old_image_path' = full path of the old image which to be reszied;
* 'new_image_path' = full path the new image to be created;
* 'width' = New Width of the image;
* 'height' = New Height of the image;
* (just for orignal) 'copy_original' = set 'true' if you like to just copy original image and you don't need to provide above 'width' and 'height', otherwise just ignore this.;
*
* @return void;
*/
private function resizing_image($data)
{
// Set copy_original=true, if you like to create original image.
$create_original = isset($data['copy_original']) && ($data['copy_original']) ? true : false;
//Set target path
$target_path = $data['new_image_path'];
//Get the width and height of upload image
list($width, $height) = getimagesize($data['old_image_path']);
if ($create_original)
{
$bigwidth = $width;
$bigheight = $height;
}
else
{
$n_wight = $data['width'];
$n_height = $data['height'];
if ($width > $n_wight) {
//If the width is greater than our maximum width preference, use the maximum width
$newwidth = $n_wight;
//Set new ratios for width and height
$newheight = ($newwidth/$width) * $height;
$bigwidth = $newwidth;
$bigheight = $newheight;
} else {
//If width is less than our maximum, use that value (so as not to pixelate the image)
$bigwidth = $width;
$bigheight = $height;
}
if ($bigheight > $n_height) {
//If the height is greater than our maximum height preference, use the maximum height
$newheight = $n_height;
//Set new ratios for width and height
$newwidth = ($newheight/$bigheight) * $bigwidth;
$bigwidth = $newwidth;
$bigheight = $newheight;
}
}
//Initiate image magick class
$im = new Imagick();
//Get image
$im->readImage( $data['old_image_path'] );
//Create thumbnail image
$im->thumbnailImage( $bigwidth, $bigheight );
//Write to file
$im->writeImage( $target_path );
/*
echo '
Original Image Size: Width-'. $width. ' Height:-'.$height;
echo '
New Image Size: Width-'. $bigwidth. ' Height:-'.$bigheight;
echo '
'.$target_path;
*/
//Destroy temp
$im->destroy();
}
This function is to resize image in various sizes
// This function is to resize image in various sizes
private function resize_image($product_id, $old_image_path)
{
define('PATH_RESIZED_PRODUCT_IMAGE', "/home/admin/assets/products/");
$new_path = PATH_RESIZED_PRODUCT_IMAGE; //"";
$product_id = 23;
$old_image_path = 'http://ecx.images-amazon.com/images/I/81RXp-hIaNL._AA1500_.jpg';
//$old_image_path = 'http://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/280px-PNG_transparency_demonstration_1.png';
/** Get File Extension. */
$path_info = pathinfo($old_image_path);
$ext = $path_info['extension'];
// Create Thumbnail (max 40px by 40px)
$new_image_path = $new_path.'t_'.$product_id.'.'.$ext; // building the new targeted file path including new filename ("";)
$data = array('old_image_path'=>$old_image_path, 'new_image_path'=>$new_image_path, 'width'=>40, 'height'=>40);
$this->resizing_image($data);
// Create Standard (max 310px by 235px)
$new_image_path = $new_path.'s_'.$product_id.'.'.$ext; // building the new targeted file path including new filename ("";)
$data = array('old_image_path'=>$old_image_path, 'new_image_path'=>$new_image_path, 'width'=>310, 'height'=>235);
$this->resizing_image($data);
// Large (max 500px by 500px)
$new_image_path = $new_path.'l_'.$product_id.'.'.$ext; // building the new targeted file path including new filename ("";)
$data = array('old_image_path'=>$old_image_path, 'new_image_path'=>$new_image_path, 'width'=>500, 'height'=>500);
$this->resizing_image($data);
// For Copying Original Image.
$new_image_path = $new_path.'orig_'.$product_id.'.'.$ext; // building the new targeted file path including new filename ("";)
$data = array('old_image_path'=>$old_image_path, 'new_image_path'=>$new_image_path, 'copy_original'=>true);
$this->resizing_image($data);
echo 'All sizes:
- t_'.$product_id.'.'.$ext.' = Thumbnail (max 40px by 40px);
- s_'.$product_id.'.'.$ext.' = Standard (max 310px by 235px);
- l_'.$product_id.'.'.$ext.' = Large (max 500px by 500px); and
- orig_'.$product_id.'.'.$ext.' = Copying Original Image,
are copied into '.PATH_RESIZED_PRODUCT_IMAGE.' path.';
die;
}
Call the above function to create different sizes image;
// Call the above function to create different sizes image;
$product_id = 18;
$old_image_path= 'http://ecx.images-amazon.com/images/I/81RXp-hIaNL._AA1500_.jpg';
resize_image($product_id, $old_image_path);