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
コード例 #1
0
 /**
  * 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('/');
         }
     }
 }
コード例 #2
0
 public function postImage(Request $request)
 {
     $validator = Validator::make($request->all(), ['height' => 'integer', 'width' => 'integer', 'image_file' => 'required|image|mimes:png,jpg,jpeg,gif,bmp']);
     if ($validator->fails()) {
         return redirect('/')->withErrors($validator)->withInput();
     }
     // if pass validation
     $image_name = $request->file('image_file')->getClientOriginalName();
     $image_extension = $request->file('image_file')->getClientOriginalExtension();
     $image_new_name = md5(microtime(true));
     $temp_file = base_path() . '/public/images/upload/' . strtolower($image_new_name . '_temp.' . $image_extension);
     $request->file('image_file')->move(base_path() . '/public/images/upload/', strtolower($image_new_name . '_temp.' . $image_extension));
     $origin_size = getimagesize($temp_file);
     $origin_width = $origin_size[0];
     $origin_height = $origin_size[1];
     // resize
     $image_resize = new ImageResize($temp_file);
     if (trim($request->get('height')) != "") {
         $height = $request->get('height');
     } else {
         $height = 0;
     }
     if (trim($request->get('width')) != "") {
         $width = $request->get('width');
     } else {
         $width = 0;
     }
     if ($width > 0 && $height > 0) {
         $image_resize->resize($width, $height);
     } else {
         if ($width == 0 && $height > 0) {
             $image_resize->resizeToHeight($height);
         } else {
             if ($width > 0 && $height == 0) {
                 $image_resize->resizeToWidth($width);
             }
         }
     }
     $image_resize->save(base_path() . '/public/images/upload/' . $image_new_name . '.' . $image_extension);
     $image_location = '/images/upload/' . $image_new_name . '.' . $image_extension;
     File::delete($temp_file);
     $image_data = array('image_name' => $image_name, 'image_extension' => $image_extension, 'image_location' => $image_location, 'origin_height' => $origin_height, 'origin_width' => $origin_width, 'height' => $height, 'width' => $width);
     $this->image_gestion->saveImage($image_data);
     return redirect('/')->with('message', 'Successfully upload image!');
 }
コード例 #3
0
ファイル: Image.php プロジェクト: mrmojorising/laraveliddig
 public function resize($pathname)
 {
     $imageresize = new ImageResize($pathname);
     $imageresize->resize($this->width, $this->height);
     $imageresize->save('imgs/' . $this->filename);
 }
コード例 #4
0
ファイル: Test.php プロジェクト: madisonmay/php-image-resize
 public function testResizeLargerNotAllowed()
 {
     $image = $this->createImage(200, 100, 'png');
     $resize = new ImageResize($image);
     $resize->resize(400, 200);
     $this->assertEquals(200, $resize->getDestWidth());
     $this->assertEquals(100, $resize->getDestHeight());
 }