public function createAction()
 {
     $this->view->disable();
     if ($this->request->hasFiles(true) == false) {
         $this->getDi()->getFlashSession()->error('Pleas select an image');
         return $this->response->redirect('admin');
     }
     if ($this->request->isPost()) {
         $productsModel = new Products();
         $productsModel->setName($this->request->getPost()['name'])->setCat($this->request->getPost()['category'])->setDescription($this->request->getPost()['description'])->setPrice($this->request->getPost()['price']);
         if (!$productsModel->save()) {
             $productsModel->setErr();
         }
         $folder = $this->request->getPost()['category'];
         if (!is_dir(__DIR__ . '/../../public/img/products/' . $folder . '/')) {
             mkdir(__DIR__ . '/../../public/img/products/' . $folder . '/', 0777);
         }
         foreach ($this->request->getUploadedFiles() as $file) {
             $galleryFileName = time() . '_' . $file->getName();
             $fullpath = __DIR__ . '/../../public/img/products/' . $folder . '/' . $galleryFileName;
             $file->moveTo($fullpath);
             $image = new \Phalcon\Image\Adapter\GD($fullpath);
             $image->resize(200, 200)->crop(200, 200);
             if (!$image->save()) {
                 var_dump($image);
                 exit;
             }
         }
         $data = array('name' => $galleryFileName, 'cat_id' => $this->request->getPost()['category'], 'product_id' => $productsModel->getId());
         //Insert record at gallery model from post rq
         $galleryModel = new Gallery();
         if (!$galleryModel->save($data)) {
             $galleryModel->setErr();
         }
     }
     //        $this->view->setVar('flash', $this->flash);
     return $this->response->redirect('admin');
 }
 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
 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.');
 }
Example #4
0
    return $response;
});
$app->post('/jadwal/update', function () use($app) {
    $robots = $app->request->getJsonRawBody();
    $robots = $app->request->getPost();
    $robots = json_decode($robots['data']);
    if ($app->request->hasFiles() == true) {
        foreach ($app->request->getUploadedFiles() as $file) {
            $filename = explode('.', $file->getName());
            $ext = $filename[count($filename) - 1];
            $string = generateRandomString();
            mkdir('public/' . $string, 0777);
            $fullimage = 'public/' . $string . '/' . $string . '.' . $ext;
            $file->moveTo($fullimage);
            $robots->gambar = $fullimage;
            $image = new \Phalcon\Image\Adapter\GD($fullimage);
            $image->resize(210, 140);
            $image->save('public/' . $string . '/' . $string . '_thumb.' . $ext);
        }
    }
    $phql = "UPDATE Jadwal SET\njudul     = :judul:,deskripsi  = :deskripsi:,gambar = :gambar:,tanggal = :tanggal:,durasi = :durasi:, alamat = :alamat:, koordinat = :koordinat:, status = :status: where id_jadwal = :id_jadwal:";
    $date = DateTime::createFromFormat('d/m/Y h:i', $robots->tanggal);
    $tanggal = $date->format('Y-m-d h:i');
    $status = $app->modelsManager->executeQuery($phql, array('judul' => $robots->judul, 'deskripsi' => $robots->deskripsi, 'gambar' => $robots->gambar, 'tanggal' => $tanggal, 'alamat' => $robots->alamat, 'koordinat' => $robots->koordinat, 'durasi' => $robots->durasi, 'status' => $robots->status, 'id_jadwal' => $robots->id_jadwal));
    //Create a response
    $response = new Response();
    //Check if the insertion was successful
    if ($status->success() == true) {
        $response->setJsonContent(array('status' => 'OK'));
    } else {
        //Change the HTTP status
Example #5
0
 public function resizeImage($img)
 {
     $image = new Phalcon\Image\Adapter\GD("images/" . $img);
     $image->resize(600, 300);
     $image->save('images/min/' . $img);
 }
Example #6
0
 private function uploadImage($model)
 {
     if ($this->request->isPost()) {
         if ($this->request->hasFiles() == true) {
             foreach ($this->request->getUploadedFiles() as $file) {
                 if (!$file->getTempName()) {
                     return;
                 }
                 if (!in_array($file->getType(), ['image/bmp', 'image/jpeg', 'image/png'])) {
                     return $this->flash->error($this->helper->at('Only allow image formats jpg, jpeg, png, bmp'));
                 }
                 $imageFilter = new \Image\Storage(['id' => $model->getId(), 'type' => 'publication']);
                 $imageFilter->removeCached();
                 $resize_x = 1000;
                 $image = new \Phalcon\Image\Adapter\GD($file->getTempName());
                 if ($image->getWidth() > $resize_x) {
                     $image->resize($resize_x);
                 }
                 $image->save($imageFilter->originalAbsPath());
                 $model->setPreviewSrc($imageFilter->originalRelPath());
                 $model->update();
                 $this->flash->success($this->helper->at('Photo added'));
             }
         }
     }
 }
Example #7
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 #8
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'));
 }