Eventviva\ImageResize::resize PHP Method

resize() public method

Resizes image according to the given width and height
public resize ( integer $width, integer $height, boolean $allow_enlarge = false ) : static
$width integer
$height integer
$allow_enlarge boolean
return static
    public function resize($width, $height, $allow_enlarge = false)
    {
        if (!$allow_enlarge) {
            // if the user hasn't explicitly allowed enlarging,
            // but either of the dimensions are larger then the original,
            // then just use original dimensions - this logic may need rethinking
            if ($width > $this->getSourceWidth() || $height > $this->getSourceHeight()) {
                $width = $this->getSourceWidth();
                $height = $this->getSourceHeight();
            }
        }
        $this->source_x = 0;
        $this->source_y = 0;
        $this->dest_w = $width;
        $this->dest_h = $height;
        $this->source_w = $this->getSourceWidth();
        $this->source_h = $this->getSourceHeight();
        return $this;
    }

Usage Example

 /**
  * Upload the image, resize and store request in DB
  *
  * @param  \Illuminate\Http\Request  $request
  */
 public function upload(Request $request)
 {
     $validator = Validator::make($request->all(), ['image' => 'required|mimes:jpeg,bmp,png', 'width' => 'required', 'height' => 'required']);
     if ($validator->fails()) {
         return redirect('/')->withErrors($validator)->withInput();
     } else {
         $_image = $request->file('image');
         $_width = $request->input('width');
         $_height = $request->input('height');
         $extension = $_image->getClientOriginalExtension();
         $fileName = time() . '_' . md5(rand(0, 9999)) . '.' . $extension;
         if ($_image->isValid()) {
             // Uploading file to given path
             $_image->move($this->upload_path, $fileName);
             // Resize the uploaded image
             $image = new ImageResize($this->upload_path . '/' . $fileName);
             $image->resize($_width, $_height);
             $image->save($this->upload_path . '/' . $fileName);
             // Save the request info
             $model = new ImageModel();
             $model->filename = $_image->getClientOriginalName();
             $model->width = $_width;
             $model->height = $_height;
             $model->save();
             // Delete the uploaded file
             $file_contents = file_get_contents($this->upload_path . '/' . $fileName);
             File::delete($this->upload_path . '/' . $fileName);
             return response($file_contents)->header('Content-Type', 'image/jpg')->header('Content-Transfer-Encoding', 'Binary')->header('Content-disposition', 'attachment; filename="' . $_image->getClientOriginalName() . '"');
         } else {
             Session::flash('error', 'Something went wrong while uploading the image.');
             return redirect('/');
         }
     }
 }
All Usage Examples Of Eventviva\ImageResize::resize