Example #1
0
 public function size($width, $height, $rect = true)
 {
     if (preg_match('/^image/', $this->mime)) {
         $filename = preg_replace('/\\.(\\w+$)/', sprintf('_%dx%d_%d.$1', $width, $height, $rect ? 1 : 0), $this->getFile());
         $destination = sprintf('%s/cache/%s', FILES_PATH, $filename);
         if (!file_exists($destination)) {
             $image = new \Phalcon\Image\Adapter\GD(sprintf('%s/%s', FILES_PATH, $this->getPath(false)));
             if ($rect) {
                 $image_w = $image->getWidth();
                 $image_h = $image->getHeight();
                 $size = $image_w < $image_h ? $image_w : $image_h;
                 $offset_x = $image_w < $image_h ? 0 : ($image_w - $image_h) / 2;
                 $offset_y = $image_w < $image_h ? ($image_h - $image_w) / 2 : 0;
                 $image->crop($size, $size, $offset_x, $offset_y)->resize($width, $height);
             } else {
                 $image->resize($width, $height);
             }
             $image->save($destination);
         }
         return sprintf('%s/cache/%s', FILES_RELATIVE_PATH, $filename);
     }
     throw new \Exception('This is not image.');
 }
 public function createThumbnail($path)
 {
     $file = str_replace(APP_PATH . '/public/images/' . $this->auth->id_reg, "", $path);
     $crop_dir = APP_PATH . '/public/images/crops/' . $this->auth->id_reg . '/200-200';
     if (!file_exists($crop_dir . '/' . $file)) {
         $new_file = $crop_dir . '/' . $file;
         // echo $crop_dir.'/'.$file;
         $raw = new Phalcon\Image\Adapter\GD($path);
         $realWidth = $raw->getWidth();
         $realHeight = $raw->getHeight();
         $cropSize = $realWidth;
         $offset_x = 0;
         $offset_y = ($realHeight - $realWidth) / 2;
         if ($realWidth > $realHeight) {
             $cropSize = $realHeight;
             $offset_x = ($realWidth - $realHeight) / 2;
             $offset_y = 0;
         }
         $handle = fopen($new_file, 'w') or die('Cannot open file:  ' . $file);
         $raw->crop($cropSize, $cropSize, $offset_x, $offset_y)->resize(200, 200);
         fwrite($handle, $raw->render());
     }
 }
Example #3
0
 /**
  * @large
  */
 public function testGD()
 {
     if (!function_exists('gd_info')) {
         $this->markTestSkipped("Skipped");
         return;
     }
     @unlink('unit-tests/assets/production/gd-new.jpg');
     @unlink('unit-tests/assets/production/gd-resize.jpg');
     @unlink('unit-tests/assets/production/gd-crop.jpg');
     @unlink('unit-tests/assets/production/gd-rotate.jpg');
     @unlink('unit-tests/assets/production/gd-flip.jpg');
     @unlink('unit-tests/assets/production/gd-sharpen.jpg');
     @unlink('unit-tests/assets/production/gd-reflection.jpg');
     @unlink('unit-tests/assets/production/gd-watermark.jpg');
     @unlink('unit-tests/assets/production/gd-mask.jpg');
     @unlink('unit-tests/assets/production/gd-background.jpg');
     // Create new image
     $image = new Phalcon\Image\Adapter\GD('unit-tests/assets/production/gd-new.jpg', 100, 100);
     $image->save();
     $this->assertTrue(file_exists('unit-tests/assets/production/gd-new.jpg'));
     $image = new Phalcon\Image\Adapter\GD('unit-tests/assets/phalconphp.jpg');
     // Resize to 200 pixels on the shortest side
     $image->resize(200, 200)->save('unit-tests/assets/production/gd-resize.jpg');
     $this->assertTrue(file_exists('unit-tests/assets/production/gd-resize.jpg'));
     $tmp = imagecreatefromjpeg('unit-tests/assets/production/gd-resize.jpg');
     $width = imagesx($tmp);
     $height = imagesy($tmp);
     $this->assertTrue($width <= 200);
     $this->assertTrue($height <= 200);
     $this->assertTrue($image->getWidth() <= 200);
     $this->assertTrue($image->getHeight() <= 200);
     // Resize to 200x200 pixels, keeping aspect ratio
     //$image->resize(200, 200, Phalcon\Image::INVERSE);
     // Resize to 500 pixel width, keeping aspect ratio
     //$image->resize(500, NULL);
     // Resize to 500 pixel height, keeping aspect ratio
     //$image->resize(NULL, 500);
     // Resize to 200x500 pixels, ignoring aspect ratio
     //$image->resize(200, 500, Phalcon\Image::NONE);
     // Crop the image to 200x200 pixels, from the center
     $image = new Phalcon\Image\Adapter\GD('unit-tests/assets/phalconphp.jpg');
     $image->crop(200, 200)->save('unit-tests/assets/production/gd-crop.jpg');
     $this->assertTrue(file_exists('unit-tests/assets/production/gd-crop.jpg'));
     $tmp = imagecreatefromjpeg('unit-tests/assets/production/gd-crop.jpg');
     $width = imagesx($tmp);
     $height = imagesy($tmp);
     $this->assertEquals($width, 200);
     $this->assertEquals($height, 200);
     $this->assertTrue($image->getWidth() == 200);
     $this->assertTrue($image->getHeight() == 200);
     // Rotate 45 degrees clockwise
     $image->rotate(45)->save('unit-tests/assets/production/gd-rotate.jpg');
     $this->assertTrue(file_exists('unit-tests/assets/production/gd-rotate.jpg'));
     $this->assertTrue($image->getWidth() > 200);
     $this->assertTrue($image->getHeight() > 200);
     // Rotate 90% counter-clockwise
     //$image->rotate(-90);
     // Flip the image from top to bottom
     $image->flip(Phalcon\Image::HORIZONTAL)->save('unit-tests/assets/production/gd-flip.jpg');
     $this->assertTrue(file_exists('unit-tests/assets/production/gd-flip.jpg'));
     // Flip the image from left to right
     //$image->flip(Phalcon\Image::VERTICAL);
     // Sharpen the image by 20%
     $image->sharpen(20)->save('unit-tests/assets/production/gd-sharpen.jpg');
     $this->assertTrue(file_exists('unit-tests/assets/production/gd-sharpen.jpg'));
     // Create a 50 pixel reflection that fades from 0-100% opacity
     $image->reflection(50)->save('unit-tests/assets/production/gd-reflection.jpg');
     $this->assertTrue(file_exists('unit-tests/assets/production/gd-reflection.jpg'));
     // Create a 50 pixel reflection that fades from 100-0% opacity
     //$image->reflection(50, 100, TRUE)->save('reflection.jpg');
     // Create a 50 pixel reflection that fades from 0-60% opacity
     //$image->reflection(50, 60, TRUE);
     // Add a watermark to the bottom right of the image
     $mark = new Phalcon\Image\Adapter\GD('unit-tests/assets/logo.png');
     $image->watermark($mark, TRUE, TRUE)->save('unit-tests/assets/production/gd-watermark.jpg');
     $this->assertTrue(file_exists('unit-tests/assets/production/gd-watermark.jpg'));
     // Mask image
     $mask = new Phalcon\Image\Adapter\GD('unit-tests/assets/logo.png');
     $image->mask($mask)->save('unit-tests/assets/production/gd-mask.jpg');
     $this->assertTrue(file_exists('unit-tests/assets/production/gd-mask.jpg'));
     // Add a text to the bottom right of the image
     $image->text('hello', TRUE, TRUE);
     // Set font size
     // $image->text('hello', TRUE, TRUE, NULL, NULL, 12);
     // Set font
     // $image->text('hello', TRUE, TRUE, NULL, NULL, 12, /usr/share/fonts/truetype/wqy/wqy-microhei.ttc);
     // Add a text to the center of the image
     //$image->text('hello');
     // Make the image background black
     $mark->background('#000')->save('unit-tests/assets/production/gd-background.jpg');
     $this->assertTrue(file_exists('unit-tests/assets/production/gd-background.jpg'));
     // Make the image background black with 50% opacity
     //$image->background('#000', 50);
     // Save the image as a PNG
     //$image->save('saved/gd.png');
     // Overwrite the original image
     //$image->save();
     // Render the image at 50% quality
     //$data = $image->render(NULL, 50);
     // Render the image as a PNG
     //$data = $image->render('png');
 }
Example #4
0
 public function testIssues2259()
 {
     if (!function_exists('gd_info')) {
         return;
     }
     $image = new Phalcon\Image\Adapter\GD('unit-tests/assets/phalconphp.jpg');
     $image->crop(100, 100, 0.5, 0.5)->save('unit-tests/assets/production/2259.jpg');
     $this->assertTrue(file_exists('unit-tests/assets/production/2259.jpg'));
     @unlink('unit-tests/assets/production/2259.jpg');
     $image->crop("100", "100", "0.5", "0.5")->save('unit-tests/assets/production/gd-2259.jpg');
     $this->assertTrue(file_exists('unit-tests/assets/production/gd-2259.jpg'));
 }