Reputation: 19
This is my post controller function. I always upload images less then 200x200
, and those images are stored in the 'upload' folder. After uploading the image, the id number is changed to something like 4546464.png
. But I want to change the image size to 60x60
when uploading and store it after changing the size and quality to 60x60x30
. This code uploads fine but not changing size and quality.
public function actionCreate() {
$model = new Monitor();
if ($model->load(Yii::$app->request->post())) {
if ($model->payment_processor) {
$model->payment_processor = implode(',', $model>payment_processor);
}
$model->file = UploadedFile::getInstance($model, 'file');
if ($model->file != '') {
$model->image = time() . '.' . $model->file->extension;
}
$model->update_at = date('Y-m-d h:i:s');
$model->save();
if ($model->file != '') {
$model->file->saveAs('upload/' . $model->image);
}
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', ['model' => $model]);
}
}
Upvotes: 1
Views: 2881
Reputation: 1695
I am using the imageprocessor extension and I'm very happy with it. You just have to add confgurations to the imageprocessor component and on save you have to call the save method of the component. It will create a new image with the size you've configured. It has a nice documentation. Give it a try.
Upvotes: 0
Reputation: 714
, you have to first upload the image in your server then resize it to what size you want , another way is before you
$model->file->saveAs('upload/' . $model->image);
resize it , but i recommend to you save the Original file and then resize it as a copy.
here is function to resize and crop image from center :
public static function resize_crop_image($max_width, $max_height, $source_file, $dst_dir, $quality = 100){
$quality = 10;
$imgsize = getimagesize($source_file);
$width = $imgsize[0];
$height = $imgsize[1];
$mime = $imgsize['mime'];
switch($mime){
case 'image/gif':
$image_create = "imagecreatefromgif";
$image = "imagegif";
break;
case 'image/png':
$image_create = "imagecreatefrompng";
$image = "imagepng";
$quality = 9;
break;
case 'image/jpeg':
$image_create = "imagecreatefromjpeg";
$image = "imagejpeg";
$quality = 100;
break;
default:
return false;
break;
}
$dst_img = imagecreatetruecolor($max_width, $max_height);
$src_img = $image_create($source_file);
$width_new = $height * $max_width / $max_height;
$height_new = $width * $max_height / $max_width;
//if the new width is greater than the actual width of the image, then the height is too large and the rest cut off, or vice versa
if($width_new > $width){
//cut point by height
$h_point = (($height - $height_new) / 2);
//copy image
imagecopyresampled($dst_img, $src_img, 0, 0, 0, $h_point, $max_width, $max_height, $width, $height_new);
}else{
//cut point by width
$w_point = (($width - $width_new) / 2);
imagecopyresampled($dst_img, $src_img, 0, 0, $w_point, 0, $max_width, $max_height, $width_new, $height);
}
$image($dst_img, $dst_dir, $quality);
if($dst_img)imagedestroy($dst_img);
if($src_img)imagedestroy($src_img);
}
use this function as easy you can :
yourModel::resize_crop_image(150,150, $path, 'upload/'.$name_you_want_or_random_string.'.jpg',$q);
the $path is a path of the original file that uploaded . you have to create a upload folder in your web app directory or change destination to where you want .
Upvotes: 0