Example #1
1
 public function apply()
 {
     if (TinifyReport::isRunning()) {
         return $this->displayError($this->l('Tinify already running.'));
     }
     $output = '';
     $report = new TinifyReport();
     // Tinify
     try {
         \Tinify\setKey(Configuration::get('TINIFY_API_KEY'));
         \Tinify\validate();
         $report->beginTinify();
         foreach (DirList::getDirList() as $dir) {
             if ($dir->enabled) {
                 $this->tinifyDirectory($dir->path, $dir->recursive, $report);
             }
         }
         $output .= $this->displayConfirmation($this->l('Tinify finished successfully.'));
     } catch (\Tinify\AccountException $e) {
         $output .= $this->logError('Verify your account limit: ' . $e->getMessage(), $report);
     } catch (\Tinify\ClientException $e) {
         $output .= $this->logError('Check your source image and request options: ' . $e->getMessage(), $report);
     } catch (\Tinify\ServerException $e) {
         $output .= $this->logError('Temporary issue with the Tinify API: ' . $e->getMessage(), $report);
     } catch (\Tinify\ConnectionException $e) {
         $output .= $this->logError('A network connection error occurred: ' . $e->getMessage(), $report);
     } catch (Exception $e) {
         $output .= $this->logError('Unrelated exception: ' . $e->getMessage(), $report);
     }
     $report->endTinify();
     if (isset($this->logger)) {
         $this->logger->logInfo('Report ' . TinifyReport::getLast(true));
     }
     return $output;
 }
 private function uploadImage($file, $location, $method, $width, $height)
 {
     $filename = 'uvis' . uniqid('', true) . '.' . $file->getClientOriginalExtension();
     \Tinify\setKey(\Config::get('services.tinify.key'));
     $image = \Tinify\fromBuffer(file_get_contents($file))->resize(array('method' => $method, 'width' => $width, 'height' => $height))->toBuffer();
     Storage::disk('s3')->put($location . '/' . $filename, $image);
     Storage::disk('s3')->setVisibility($location . '/' . $filename, 'public');
     return 'http://d4p17temvc0sy.cloudfront.net/' . $location . '/' . $filename;
 }
Example #3
0
 /**
  * Store a newly created resource in storage.
  *
  * @param SlidersRequest|Request $request
  * @return \Illuminate\Http\Response
  */
 public function store(SlidersRequest $request)
 {
     $file = $request->file('picture');
     $filename = 'oshu' . uniqid('', true) . '.' . $file->getClientOriginalExtension();
     \Tinify\setKey(\Config::get('services.tinify.key'));
     $image = \Tinify\fromBuffer(file_get_contents($file))->resize(array('method' => 'scale', 'height' => 299))->toBuffer();
     Storage::disk('s3')->put('oshu/' . $filename, $image);
     Storage::disk('s3')->setVisibility('oshu/' . $filename, 'public');
     $slider = new Slider();
     $slider->title = $request->title;
     $slider->picture = 'http://cdn.oshu.nl/' . $filename;
     $slider->save();
     Flash::success('Slider opgeslagen');
     return redirect('admin/sliders');
 }
Example #4
0
 public function upload(Request $request)
 {
     $file = $request->file('file');
     $v = Validator::make($request->all(), ['file' => 'required|mimes:jpeg,jpg,png|max:10000']);
     if ($v->fails()) {
         return Response::json(['error' => $v->errors()]);
     }
     //Use some method to generate your filename here. Here we are just using the ID of the image
     $filename = 'oshu' . uniqid('', true) . '.' . $file->getClientOriginalExtension();
     //Push file to S3
     \Tinify\setKey(\Config::get('services.tinify.key'));
     $image = \Tinify\fromBuffer(file_get_contents($file))->resize(array('method' => 'cover', 'width' => 175, 'height' => 175))->toBuffer();
     $move = Storage::disk('s3')->put('oshu/' . $filename, $image);
     Storage::disk('s3')->setVisibility('oshu/' . $filename, 'public');
     if ($move) {
         return Response::json(['file' => ['filelink' => ['http://cdn.oshu.nl/' . $filename]]]);
     } else {
         return Response::json(['error' => true]);
     }
 }
Example #5
0
 public function testFromBufferShouldReturnSource()
 {
     CurlMock::register("https://api.tinify.com/shrink", array("status" => 201, "headers" => array("Location" => "https://api.tinify.com/some/location")));
     Tinify\setKey("valid");
     $this->assertInstanceOf("Tinify\\Source", Tinify\fromBuffer("png file"));
 }
Example #6
0
 /**
  * Set Tiny API Key
  *
  * @return void
  */
 function __construct($api_key)
 {
     \Tinify\setKey($api_key);
 }
Example #7
0
 public static function setUpBeforeClass()
 {
     \Tinify\setKey(getenv("TINIFY_KEY"));
     $unoptimizedPath = __DIR__ . "/examples/voormedia.png";
     self::$optimized = \Tinify\fromFile($unoptimizedPath);
 }
Example #8
0
 /**
  * Reduce image size and optimize the image quality
  * 
  * @TODO Find an faster image optimization solution
  * @author salvipascual
  * @param String $imagePath, path to the image
  * */
 public function optimizeImage($imagePath, $width = false, $height = false)
 {
     \Tinify\setKey("XdzvHGYdXUpiWB_fWI2muKXgV3GZVXjq");
     // load and optimize image
     $source = \Tinify\fromFile($imagePath);
     // scale image based on width
     if ($width && !$height) {
         $source = $source->resize(array("method" => "scale", "width" => $width));
     }
     // scale image based on width
     if (!$width && $height) {
         $source = $source->resize(array("method" => "scale", "height" => $height));
     }
     if ($width && $height) {
         $source = $source->resize(array("method" => "cover", "width" => $width, "height" => $height));
     }
     // save the optimized file
     $source->toFile($imagePath);
 }
Example #9
0
 public function testToFileShouldStoreImageData()
 {
     CurlMock::register("https://api.tinify.com/shrink", array("status" => 201, "headers" => array("Location" => "https://api.tinify.com/some/location")));
     CurlMock::register("https://api.tinify.com/some/location", array("status" => 200, "body" => "compressed file"));
     $path = tempnam(sys_get_temp_dir(), "tinify-php");
     Tinify\setKey("valid");
     Tinify\Source::fromBuffer("png file")->toFile($path);
     $this->assertSame("compressed file", file_get_contents($path));
 }
Example #10
0
 function setUp()
 {
     Tinify\setKey(NULL);
 }
Example #11
-1
function png2Tiny($pngName, $tinyPng)
{
    \Tinify\setKey("_CU-TGu6JmRws3B9ONb90bcfIcHveuMQ");
    $source = \Tinify\fromFile($pngName);
    $newFile = $dstPath . $pngName;
    $source->toFile($tinyPng);
}
 protected function __construct($api_key, $after_compress_callback)
 {
     parent::__construct($after_compress_callback);
     $this->proxy = new WP_HTTP_Proxy();
     \Tinify\setAppIdentifier(self::identifier());
     \Tinify\setKey($api_key);
 }