12:30 AM

Resize image to squared in CakePHP

Posted by Reinhart |

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

<?php

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;

}

}?&rt;

<?php 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;

}

}
?&rt;

12:26 AM

Upload and Resize Image in CakePHP

Posted by Reinhart |

I have spent many hours to see articles about resizing and uploading image in cakePHP, almost of them are sounding sucks for me like resizing on View by accessing or injecting the url or copy paste by another written without testing code. Event I was trying to convert AkImage from Akelos (Closer to ImageMagick in Rails) but got bunch of errors.

Any way i got article from sobbour website, but i am not satisfied with the code because the component automatically resize for big and thumb by 2 method directly, it will make bounced my hosting with trash image. And some lines were not realible any more with CakePHP, here is my modified Code (save it to /appName/controller/components/image.php)


<?php
class ImageComponent extends Object
{
var $contentType = array('image/jpg','image/bmp','image/jpeg','image/gif','image/png','image/pjpg','image/pbmp','image/pjpeg','image/ppng','image/pgif');
function upload_image_and_thumbnail($fileData,$size,$subFolder,$prefix) {
if (strlen($fileData['name'])>4)
{
$error = 0;
$destFolder = WWW_ROOT.$subFolder;
$realFileName = $fileData['name'];
if(!is_dir($destFolder)) mkdir($destFolder,true);
$filetype = $this->getFileExtension($fileData['name']);
$filetype = strtolower($filetype);
if(!in_array($fileData['type'],$this->contentType)){
return false;exit();
}
else if($fileData['size'] > 700000 ){
return false;exit();
}
else
{
$imgsize = GetImageSize($fileData['tmp_name']);
}
if (is_uploaded_file($fileData['tmp_name']))
{
if (!copy($fileData['tmp_name'],$destFolder.'/'.$realFileName ))
{
return false;
exit();
}
else {
$this->resize_img($destFolder.'/'.$realFileName, $size, $destFolder.'/'.$prefix.$realFileName);
unlink($destFolder.'/'.$realFileName);
}
}
return $fileData;
}
}
function delete_image($filename)
{
unlink($filename);
}
function resize_img($tempFile, $size, $newFile)
{
$filetype = $this->getFileExtension($tempFile);
$filetype = strtolower($filetype);
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;
}
$true_width = imagesx($img_src);
$true_height = imagesy($img_src);

$size = explode('x',strtolower($size));
if ($true_width>=$true_height)
{
$width=$size[0];
$height = ($width/$true_width)*$true_height;
}
else
{
$height=$size[1];
$width = ($height/$true_height)*$true_width;
}
$img_des = imagecreatetruecolor($width,$height);
imagecopyresampled ($img_des, $img_src, 0, 0, 0, 0, $width, $height, $true_width, $true_height);
// Save the resized image
switch($filetype)
{
case "jpeg":
case "jpg":
imagejpeg($img_des,$newFile,80);
break;
case "gif":
imagegif($img_des,$newFile,80);
break;
case "png":
imagepng($img_des,$newFile,80);
case "bmp":
imagewbmp($img_des,$newFile,80);
break;
}
}
function getFileExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
}
?>



<?php

class ImageComponent extends Object

{

var $contentType = array('image/jpg','image/bmp','image/jpeg','image/gif','image/png','image/pjpg','image/pbmp','image/pjpeg','image/ppng','image/pgif');

function upload_image_and_thumbnail($fileData,$size,$subFolder,$prefix) {

if (strlen($fileData['name'])>4)

{

$error = 0;

$destFolder = WWW_ROOT.$subFolder;

$realFileName = $fileData['name'];

if(!is_dir($destFolder)) mkdir($destFolder,true);

$filetype = $this->getFileExtension($fileData['name']);

$filetype = strtolower($filetype);

if(!in_array($fileData['type'],$this->contentType)){

return false;exit();

}

else if($fileData['size'] > 700000 ){

return false;exit();

}

else

{

$imgsize = GetImageSize($fileData['tmp_name']);

}

if (is_uploaded_file($fileData['tmp_name']))

{

if (!copy($fileData['tmp_name'],$destFolder.'/'.$realFileName ))

{

return false;

exit();

}

else {

$this->resize_img($destFolder.'/'.$realFileName, $size, $destFolder.'/'.$prefix.$realFileName);

unlink($destFolder.'/'.$realFileName);

}

}

return $fileData;

}

}

function delete_image($filename)

{

unlink($filename);

}

function resize_img($tempFile, $size, $newFile)

{

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

$filetype = strtolower($filetype);

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;

}

$true_width = imagesx($img_src);

$true_height = imagesy($img_src);



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

if ($true_width>=$true_height)

{

$width=$size[0];

$height = ($width/$true_width)*$true_height;

}

else

{

$height=$size[1];

$width = ($height/$true_height)*$true_width;

}

$img_des = imagecreatetruecolor($width,$height);

imagecopyresampled ($img_des, $img_src, 0, 0, 0, 0, $width, $height, $true_width, $true_height);

// Save the resized image

switch($filetype)

{

case "jpeg":

case "jpg":

imagejpeg($img_des,$newFile,80);

break;

case "gif":

imagegif($img_des,$newFile,80);

break;

case "png":

imagepng($img_des,$newFile,80);

case "bmp":

imagewbmp($img_des,$newFile,80);

break;

}

}

function getFileExtension($str)

{

$i = strrpos($str,".");

if (!$i) { return ""; }

$l = strlen($str) - $i;

$ext = substr($str,$i+1,$l);

return $ext;

}

}

?>

And in your class instance method , run this line

<?php
class DeJavu extends Appcontroller {
var $components = array("Image");
function add(){
....any code....
$this->Image->upload_image_and_thumbnail($this->data['Webinfo']['file'],'150x150','img','logo_');
....any code....
}
}
?>

now I am happy to see my app can resize image without annoying script any more. Good Luck for you too.

A few days ago i have problem how to implement my live streaming using SharedObject in Red5 because usually SharedObject, as i know, only good implementable for local only (like getting or store cookies), before implemented to my live streaming application, i created simple chat application to remote SharedObject :

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="creationComplete()">
<mx:Script source="streaming.as"/>
<mx:TextInput x="10" y="10" id="rtmpUrl" text="your rtmp address"/>
<mx:Button x="216" y="10" id="connectBtn"/>
<mx:TextInput x="10" y="269" id="enterMsg" />
<mx:TextArea x="10" y="40" height="221" id="msgBoard" width="222" />
<mx:Button x="178" y="269" id="sendBtn"/>
</mx:Application>

And now let create streaming.as script :
import flash.events.MouseEvent;
import flash.events.NetStatusEvent;
import flash.events.SyncEvent;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.net.SharedObject;

import mx.controls.Alert;

public var NetCont:NetConnection;
public var NetStrem:NetStream
public var SharObj:SharedObject;

public function creationComplete():void{
connectBtn.label = "Connect";
connectBtn.addEventListener(MouseEvent.CLICK, ConnectServer);

sendBtn.label = "Send";
sendBtn.addEventListener(MouseEvent.CLICK, SendChat);
}

public function ConnectServer(event:MouseEvent):void{
NetCont = new NetConnection();
NetCont.client = new NetConnectionClient;
NetCont.connect(rtmpUrl.text);
NetCont.addEventListener(NetStatusEvent.NET_STATUS, createSharedObject);
}

public function createSharedObject(event:NetStatusEvent):void{

if(NetCont.connected){
SharObj = SharedObject.getRemote("SampleMessage",NetCont.uri,false,false);
SharObj.connect(NetCont);
SharObj.addEventListener(SyncEvent.SYNC, SharObjView);


}else{

Alert.show('Unable to Connect Internet or Rtmp Address');


}
}


public function SharObjView(event:SyncEvent):void{
msgBoard.htmlText += SharObj.data['SampleMessage']+"<br>";

}

public function SendChat(event:MouseEvent):void{
SharObj.setProperty("SampleMessage",enterMsg.text);
}

There are 2 optional for using SharedObject in ActionScript specially for flex SharedObject.getLocal() method or the SharedObject.getRemote()

What we use for internet connection is using getRemote. And here is code for next connection client :
package
{
public class NetConnectionClient
{
public function NetConnectionClient()
{
}
public function onBWDone(... rest):void
{

}
public function onBWCheck(... rest):uint
{
//have to return something, so returning anything :)
return 0;
}

public function onMetaData(info:Object):void {
trace("metadata: duration=" + info.duration + " width=" + info.width + " height=" + info.height + " framerate=" + info.framerate);
}
}
}

Subscribe