768x90 Getting Online Shouldn't be Tough- $7.49 .com Domains at Go Daddy

Resize image to squared in CakePHP

The code is almost closer with my previous article, just change function resize_image to square_image


function crop_img($imgname, $scale, $filename) {

$filetype = $this->getFileExtension($imgname);

$filetype = strtolower($filetype);


switch($filetype){


case "jpeg":


case "jpg":


$img_src = ImageCreateFromjpeg ($imgname);


break;


case "gif":


$img_src = imagecreatefromgif ($imgname);


break;


case "png":


$img_src = imagecreatefrompng ($imgname);


break;


}


$width = imagesx($img_src);


$height = imagesy($img_src);


$ratiox = $width / $height * $scale;


$ratioy = $height / $width * $scale;


//-- Calculate resampling


$newheight = ($width <= $height) ? $ratioy : $scale;


$newwidth = ($width <= $height) ? $scale : $ratiox;


//-- Calculate cropping (division by zero)


$cropx = ($newwidth - $scale != 0) ? ($newwidth - $scale) / 2 : 0;


$cropy = ($newheight - $scale != 0) ? ($newheight - $scale) / 2 : 0;


//-- Setup Resample & Crop buffers


$resampled = imagecreatetruecolor($newwidth, $newheight);


$cropped = imagecreatetruecolor($scale, $scale);


//-- Resample


imagecopyresampled($resampled, $img_src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);


//-- Crop


imagecopy($cropped, $resampled, 0, 0, $cropx, $cropy, $newwidth, $newheight);


// Save the cropped image


switch($filetype)


{


case "jpeg":


case "jpg":


imagejpeg($cropped,$filename,80);


break;


case "gif":


imagegif($cropped,$filename,80);


break;


case "png":


imagepng($cropped,$filename,80);


break;


}


}

function crop_img($tempFile, $scale, $newFile) {

$filetype = $this->getFileExtension($tempFile);

$filetype = strtolower($tempFile);

switch($filetype) {

case "jpeg":

case "jpg":

 $img_src = imagecreatefromjpeg($tempFile);

 break;

case "gif":

$img_src = imagecreatefromgif ($tempFile);

break;

case "png":

$img_src = imagecreatefrompng ($tempFile);

case "bmp":

$img_src = imagecreatefromwbmp ($tempFile);

break;

}

$width = imagesx($img_src);

$height = imagesy($img_src);

$scale = explode('x',strtolower($size));

$ratiox = $width / $height * $scale[0];

$ratioy = $height / $width * $scale[0];

//-- Calculate resampling

$newheight = ($width <= $height) ? $ratioy : $scale[0];

$newwidth = ($width <= $height) ? $scale[0] : $ratiox;

//-- Calculate cropping (division by zero)

$cropx = ($newwidth - $scale != 0) ? ($newwidth - $scale) / 2 : 0;

$cropy = ($newheight - $scale != 0) ? ($newheight - $scale) / 2 : 0;

//-- Setup Resample & Crop buffers

$resampled = imagecreatetruecolor($newwidth, $newheight);

$cropped = imagecreatetruecolor($scale[0], $scale[0]);

//-- Resample

imagecopyresampled($resampled, $img_src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

//-- Crop

imagecopy($cropped, $resampled, 0, 0, $cropx, $cropy, $newwidth, $newheight);

// Save the cropped image

switch($filetype)

{

case "jpeg":

case "jpg":

imagejpeg($cropped,$newFile,80);

break;

case "gif":

imagegif($cropped,$newFile,80);

break;

case "png":

imagepng($cropped,$newFile,80);

break;

}

}

No Response to "Resize image to squared in CakePHP"

 
powered by Blogger