Example #1
0
 /** Usage example: {{ product.image | transparencyThumb(180,180, {color: [255,0,0]}) }}
  * @param File  $originalImage
  * @param       $width
  * @param       $height
  * @param array $opts
  * @return string
  */
 public function transparencyThumb($originalImage, $width, $height, $opts = [])
 {
     if ($originalImage == null) {
         return null;
     }
     $extension = $originalImage->getAttribute('extension');
     if ($extension == 'png' || $extension == 'gif') {
         if (isset($opts['color']) == false) {
             $opts['color'] = [255, 255, 255];
         }
         $appendix = '_trans_tmp_' . implode('-', $opts['color']) . '.jpg';
         $dbDiskName = str_replace('.' . $extension, $appendix, $originalImage->getAttribute('file_name'));
         $dbAttachmentType = $originalImage->getAttribute('attachment_type') . '::transparency';
         $dbFile = File::where('file_name', $dbDiskName)->where('attachment_type', $dbAttachmentType)->first();
         if ($dbFile) {
             return $dbFile->getThumb($width, $height, $opts);
         }
         if ($extension == 'png') {
             $tmpImageData = imagecreatefrompng(base_path() . $originalImage->getPath());
         } else {
             $tmpImageData = imagecreatefromgif(base_path() . $originalImage->getPath());
         }
         $imageWidth = imagesx($tmpImageData);
         $imageHeight = imagesy($tmpImageData);
         $backgroundImg = imagecreatetruecolor($imageWidth, $imageHeight);
         $color = imagecolorallocate($backgroundImg, $opts['color'][0], $opts['color'][1], $opts['color'][2]);
         imagefill($backgroundImg, 0, 0, $color);
         imagecopy($backgroundImg, $tmpImageData, 0, 0, 0, 0, $imageWidth, $imageHeight);
         $tmpImagePath = $originalImage->getTempPath() . '/' . $dbDiskName;
         imagejpeg($backgroundImg, $tmpImagePath);
         imagedestroy($backgroundImg);
         imagedestroy($tmpImageData);
         //return str_replace(base_path(), '', $tmpImagePath);
         $file = File::create(['data' => $tmpImagePath, 'attachment_type' => $dbAttachmentType]);
         return $file->getThumb($width, $height, $opts);
     }
     return $originalImage->getThumb($width, $height, $opts);
 }