public function saveAs($type, $text, $file) { $fontfile = __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "Resources" . DIRECTORY_SEPARATOR . "fonts" . DIRECTORY_SEPARATOR . 'Lato-Regular.ttf'; @unlink($file); switch ($type) { case $type == 99: include_once __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "Resources" . DIRECTORY_SEPARATOR . "phpqrcode" . DIRECTORY_SEPARATOR . "qrlib.php"; \QRcode::png($text, $file, QR_ECLEVEL_L, 12); break; case $type == 90: $font = new \Imagine\Gd\Font($fontfile, 35, new \Imagine\Image\Color('fff', 100)); $resource = imagecreatetruecolor(2000, 60); $color = new \Imagine\Image\Color('fff'); $white = imagecolorallocate($resource, 255, 255, 255); $black = imagecolorallocate($resource, 0, 0, 0); if (false === $white) { throw new RuntimeException('Unable to allocate color'); } if (false === imagefill($resource, 0, 0, $white)) { throw new RuntimeException('Could not set background color fill'); } imagettftext($resource, 35, 0, 10, 50, $black, $fontfile, $text); $image = new Image($resource); $image->crop(new \Imagine\Image\Point(0, 0), new \Imagine\Image\Box(20 + $font->box($text)->getWidth(), 60)); $image->save($file); break; case is_numeric($type): $type = $this->types[$type]; default: $validator = new BarcodeValidator(array('adapter' => $type, 'usechecksum' => false)); // if (!$validator->isValid($text)) { // $message = implode("\n", $validator->getMessages()); // throw new \Symfony\Component\HttpKernel\Exception\HttpException(401, $message, null); // } //z apki dostaje barcody z // if($type == 'ean13') // { // $text = substr($text, 0, -1); // } $barcodeOptions = array('text' => $text, 'factor' => 3, 'font' => __DIR__ . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "Resources" . DIRECTORY_SEPARATOR . "fonts" . DIRECTORY_SEPARATOR . 'Lato-Regular.ttf'); $rendererOptions = array(); $imageRenderer = Barcode::factory($type, 'image', $barcodeOptions, $rendererOptions, false); //fix to not throw error when try to render barcode with code with checksum if ($imageRenderer->getBarcode()->getWithChecksum()) { //maybe i got barcode without checksum need to test it by try cache :( try { $imageRenderer->getBarcode()->validateText($text); } catch (\Exception $exc) { //propably length error remove checksum //when barcode have mandatoryChecksum and have default //validateSpecificText then renderer waiting for code without checksum :( $imageRenderer->getBarcode()->setText(substr($text, 0, -1)); } } //catch error and send http error 400 not 500 as default try { $image = new Image($imageRenderer->draw()); } catch (\Exception $exc) { $message = $exc->getMessage(); throw new \Symfony\Component\HttpKernel\Exception\HttpException(400, $message, null); } $image->save($file); } return true; }
/** * this method crops the image * @param Array $crop crop config * @param \Imagine\Gd\Image $image */ protected function createCrop($crop, $image) { if (!empty($crop['cropped_field'])) { $save_path = $this->getUploadPath($crop['cropped_field']); } else { $save_path = $this->getUploadPath($this->attribute); } $sizes = explode('-', $crop['value']); $real_size = $image->getSize(); foreach ($sizes as $ind => $cr) { $sizes[$ind] = round($sizes[$ind] * ($ind % 2 == 0 ? $real_size->getWidth() : $real_size->getHeight()) / 100); } $crop_image = $image->crop(new Point($sizes[0], $sizes[1]), new Box($sizes[2] - $sizes[0], $sizes[3] - $sizes[1])); if (!empty($crop['crop_width'])) { $crop_image = $crop_image->resize(new Box($crop['crop_width'], $crop['crop_width'] / $crop['ratio'])); } $crop_image->save($save_path, isset($crop['save_options']) ? $crop['save_options'] : $this->save_options); }