I wrote the below code ( searched Google and got here and there ) . This is working perfect . But do you guys see any issues with this approach ? Or any better way to handle it ? There are good number of plugins , but I wanted this to be very very light and came up with this approach.
//view
$this->widget('ext.MUploadify',array(
'name'=>'Photo',
'buttonText'=>'Browse Photos',
'multi' => true,
'script'=>array('mailBox/upload'),
'fileExt' => '*.jpg;*.png;',
'method' => 'post',
'uploadLimit' => '1',
'uploadButtonText' =>'Upload Photos'
));
//Action
public function actionUpload() {
if (isset($_POST['Photo'])) {
$user_id=10;
$album_id=15;
$filename=$_POST['Filename'] ;
$imageoriginal = CUploadedFile::getInstanceByName('Photo');
$tempFile = $imageoriginal->getTempName();
$fileName = $filename;
$targetPath = $_SERVER['DOCUMENT_ROOT'].date('Y'). '/'.date('m'). '/'.date('d'). '/'.$user_id. '/'.$album_id. '/' ;
$targetFile = str_replace('//','/',$targetPath) . $fileName;
if (!is_dir($targetPath)) {
mkdir($targetPath,0755,true);
}
$extension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
if($extension=="jpg" || $extension=="jpeg" )
{
$src = imagecreatefromjpeg($tempFile);
}
else if($extension=="png")
{
$src = imagecreatefrompng($tempFile);
}
else
{
$src = imagecreatefromgif($tempFile);
}
list($width,$height)=getimagesize($tempFile);
$newwidth=60;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
$newwidth1=25;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
$filename = str_replace('//','/',$targetPath) . 'thumb_'.$fileName;
$filename1 = str_replace('//','/',$targetPath) . 'small_'.$fileName;
imagejpeg($tmp,$filename,100);
imagejpeg($tmp1,$filename1,100);
imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
move_uploaded_file($tempFile,$targetFile);
echo 1;
Yii::app()->end();
}
return false;
}
Thank You
Regards
Yii Fan