public function postUpload($id = '') { $user = Auth::getUser(); $record = Ad::whereRaw('id = ? AND user_id = ?', array($id, $user->id))->first(); if (!$record) { return Response::json(array('status' => 'error', 'message' => trans(CLF_LANG_MESSAGE . 'user_not_perm')), 500); } // Check limit photos $count = File::whereRaw('attachment_id = ? AND attachment_type = ?', array($id, 'DLNLab\\Classified\\Models\\Ad'))->count(); if ($count >= CLF_LIMIT_AD_PHOTO) { return Response::json(array('status' => 'error', 'message' => trans(CLF_LANG_MESSAGE . 'user_not_perm')), 500); } $result = null; if (Input::hasFile('file_data')) { try { $uploadedFile = Input::file('file_data'); $validationRules = ['max:' . File::getMaxFilesize()]; $validationRules[] = 'mimes:jpg,jpeg,bmp,png,gif'; $validation = Validator::make(['file_data' => $uploadedFile], ['file_data' => $validationRules]); if ($validation->fails()) { throw new ValidationException($validation); } if (!$uploadedFile->isValid()) { throw new SystemException('File is not valid'); } $file = new File(); $file->data = $uploadedFile; $file->field = 'ad_images'; $file->attachment_id = $id; $file->attachment_type = 'DLNLab\\Classified\\Models\\Ad'; $file->is_public = true; $file->save(); $file->thumb = $file->getThumb(250, 250, ['mode' => 'crop']); $result = new \stdClass(); $result->id = $file->id; } catch (Exception $ex) { $result = $ex->getMessage(); return Response::json(array('status' => 'error', 'message' => $result), 500); } } $result->photo_pattern = '<div id="dln_photo_item_' . $file->id . '" data-id="' . $file->id . '" class="dln-photo-item panel panel-default bg-master-lightest sm-m-b-5 sm-m-l-5 sm-m-r-5"> <div class="panel-body sm-p-t-10 sm-p-l-5 sm-p-r-5 sm-p-b-10"> <div class="col-xs-12 col-sm-3 sm-p-l-5 sm-p-r-5"> <img class="img-thumbnail m-b-5" width="100%" src="' . $file->thumb . '"> </div> <div class="col-xs-12 col-sm-9 sm-p-l-5 sm-p-r-5"> <textarea placeholder="' . trans(CLF_LANG_LABEL . 'noi_dung') . '" id="dln_photo_desc" class="form-control clearfix m-b-10" required maxlength="500">' . $file->desc . '</textarea> <a href="javascript:void(0)" class="dln-up-photo btn btn-sm btn-default pull-left m-r-5" data-original-title="' . trans(CLF_LANG_LABEL . 'len') . '" data-toggle="tooltip"><i class="fs-14 fa fa-arrow-up"></i></a> <a href="javascript:void(0)" class="dln-down-photo btn btn-sm btn-default pull-left" data-original-title="' . trans(CLF_LANG_LABEL . 'xuong') . '" data-toggle="tooltip"><i class="fs-14 fa fa-arrow-down"></i></a> <a href="javascript:void(0)" class="dln-delete-photo btn btn-sm btn-danger pull-right" data-original-title="' . trans(CLF_LANG_LABEL . 'xoa') . '" data-toggle="tooltip"><i class="fs-14 fa fa-trash-o"></i></a> </div> </div> </div>'; return Response::json(response_message(200, $result)); }
/** 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); }