Is there a way to upload a phot and generate a thumbnail from that in yii?
Is there a way to upload a phot and generate a thumbnail from that in yii?
I’m just a newby, but here’s how I solved it:
In my model Foto:
public function saveThumb($image,$location)
{
$this->saveResizedImage($image,100,$location);
}
public function savePreview($image,$location)
{
$this->saveResizedImage($image,225,$location);
}
public function saveResizedImage($image, $height, $location)
{
$file = imagecreatefromjpeg($image);
//Haal de originele breedte en hoogte op
$orgwidth = imagesx($file);
$orgheight = imagesy($file);
//Bereken de nieuwe breedte aan de hand van de nieuwe hoogte
$width = ($height/$orgheight)*$orgwidth;
//Maak de nieuwe afbeelding
$resized = ImageCreateTrueColor($width, $height);
//Vul de thumb met de verkleinde foto
imagecopyResampled($resized, $file, 0, 0, 0, 0, $width, $height, $orgwidth, $orgheight);
//sla de nieuwe afbeelding op
imagejpeg($resized,$location);
//verwijder data uit geheugen
imagedestroy($file);
imagedestroy($resized);
}
In my controller:
public function actionCreate()
{
$model = new Foto;
// Uncomment the following line if AJAX validation is needed
//$this->performAjaxValidation($model);
if(isset($_POST['Foto']))
{
$model->attributes=$_POST['Foto'];
$model->image=CUploadedFile::getInstance($model,'image');
if($model->save())
{
//sla origineel bestand op
$model->image->saveAs(Yii::app()->basePath . '/../images/full/' . $model->id . '.jpg');
$model->image = Yii::app()->basePath . '/../images/full/' . $model->id . '.jpg';
//Sla preview bestand op
$model->savePreview($model->image, Yii::app()->basePath . '/../images/preview/' . $model->id . '.jpg');
//Sla thumb op
$model->saveThumb($model->image, Yii::app()->basePath . '/../images/thumb/' . $model->id . '.jpg');
}