Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
ImageFileModelUtil.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
41  {
48  public static function readImageFromCache($fileName, $shouldGetThumbnail = false)
49  {
50  assert('is_string($fileName)');
51  assert('is_bool($shouldGetThumbnail)');
52  $imagePath = ImageFileModel::getImageCachePathByFileName($fileName, $shouldGetThumbnail);
53  if (!file_exists($imagePath))
54  {
55  $imageFileModel = ImageFileModel::getByFileName($fileName);
56  $imageFileModel->createImageCache($shouldGetThumbnail);
57  }
58  $mime = ZurmoFileHelper::getMimeType($imagePath);
59  $size = filesize($imagePath);
60  $name = pathinfo($imagePath, PATHINFO_FILENAME);
61  header('Content-Type: ' . $mime);
62  header('Content-Length: ' . $size);
63  header('Content-Name: ' . $name);
64  readfile($imagePath);
65  Yii::app()->end(0, false);
66  }
67 
68  public static function getUrlForActionUpload()
69  {
70  return Yii::app()->createAbsoluteUrl('zurmo/imageModel/upload');
71  }
72 
73  public static function getUrlForActionGetUploaded()
74  {
75  return Yii::app()->createAbsoluteUrl('zurmo/imageModel/getUploaded');
76  }
77 
78  public static function getUrlForGetImageFromImageFileName($fileName, $shouldReturnForThumbnail = false)
79  {
80  assert('is_string($fileName)');
81  assert('is_bool($shouldReturnForThumbnail)');
82  $path = 'zurmo/imageModel/getImage';
83  if ($shouldReturnForThumbnail)
84  {
85  $path = 'zurmo/imageModel/getThumb';
86  }
87  return Yii::app()->createAbsoluteUrl($path, array('fileName' => $fileName));
88  }
89 
90  public static function getImageSummary(ImageFileModel $imageFileModel, $layout = null)
91  {
92  $data = array();
93  if ($layout == null)
94  {
95  $layout = static::getDefaultLayout();
96  }
97  $imagePath = ImageFileModel::getImageCachePathByFileName($imageFileModel->getImageCacheFileName(), false);
98  if (!file_exists($imagePath))
99  {
100  $imageFileModel->createImageCache();
101  }
102  static::resolveWidthAndHeightAttributesIfTheyAreMissing($imageFileModel);
103  $url = static::getUrlForGetImageFromImageFileName($imageFileModel->getImageCacheFileName(), true);
104  $urlForPreview = Yii::app()->createAbsoluteUrl('zurmo/imageModel/modalPreview', array('fileName' => $imageFileModel->getImageCacheFileName()));
105  $data['{image}'] = ZurmoHtml::image($url, '', array('data-url' => $urlForPreview));
106  $data['{name}'] = $imageFileModel->name;
107  $data['{size}'] = FileModelDisplayUtil::convertSizeToHumanReadableAndGet((int) $imageFileModel->size);
108  $data['{dimensions}'] = $imageFileModel->width . ' × ' . $imageFileModel->height;
109  $data['{creator}'] = $imageFileModel->createdByUser;
110  $data['{createdTime}'] = DateTimeUtil::convertDbFormattedDateTimeToLocaleFormattedDisplay($imageFileModel->createdDateTime);
111  $data['{selectLink}'] = static::getSelectLink();
112  $data['{editLink}'] = static::getEditLink();
113  return strtr($layout, $data);
114  }
115 
116  protected static function resolveWidthAndHeightAttributesIfTheyAreMissing(ImageFileModel $imageFileModel)
117  {
118  if ($imageFileModel->width == null && $imageFileModel->height == null)
119  {
120  $imageProperties = getimagesize($imageFileModel->getImageCachePath());
121  $imageFileModel->width = $imageProperties[0];
122  $imageFileModel->height = $imageProperties[1];
123  $imageFileModel->save();
124  }
125  }
126 
127  protected static function getEditLink()
128  {
129  $editText = Zurmo::t('Core', 'Edit');
130  return static::getLink($editText, ImageElement::IMAGE_EDIT_LINK_CLASS_NAME, 'simple-link');
131  }
132 
133  protected static function getSelectLink()
134  {
135  $linkText = Zurmo::t('ZurmoModule', 'Change');
136  return static::getLink($linkText, ImageElement::IMAGE_SELECT_LINK_CLASS_NAME, 'simple-link');
137  }
138 
139  protected static function resolveInsertLink($imageFileModel)
140  {
141  $summary = static::getImageSummary($imageFileModel);
142  return "javascript:parent.transferModalImageValues({$imageFileModel->id}, '{$summary}')";
143  }
144 
145  public static function getLink($linkText, $class, $type)
146  {
147  assert('is_string($type)');
148  if ($type == 'simple-link')
149  {
150  $content = ZurmoHtml::link($linkText, '#', array('class' => 'simple-link ' . $class));
151  }
152  else
153  {
154  $content = ZurmoHtml::link(
155  '<span class="z-spinner"></span>' . ZurmoHtml::tag( 'span', array( 'class' => 'z-label' ), $linkText ),
156  '#',
157  array( 'class' => 'secondary-button ' . $class ) );
158  }
159  return $content;
160  }
161 
162  protected static function getDefaultLayout()
163  {
164  $createdByLabel = Zurmo::t('ZurmoModule', 'Created by');
165  $onLabel = Zurmo::t('ZurmoModule', 'on');
166  return '<div class="builder-uploaded-image-thumb">{image}</div>'.
167  '<div class="image-links">{selectLink} · {editLink}</div>'.
168  '<div class="builder-image-details">' .
169  '<strong>{name}</strong><br />{size} · {dimensions} · ' . $createdByLabel .
170  ' {creator} ' . $onLabel . ' {createdTime}</div>';
171  }
172 
173  public static function getImageFileNameWithDimensions($imageFileName, $width, $height)
174  {
175  assert('is_string($imageFileName)');
176  assert('is_int($width)');
177  assert('is_int($height)');
178  $imageFileName = preg_replace("/^\d.*x\d.*\s/", "", $imageFileName);
179  $imageFileName = $width . 'x' . $height . ' ' . $imageFileName;
180  return $imageFileName;
181  }
182 
183  public static function getImageFromHtmlImgTag($htmlContent)
184  {
185  assert('is_string($htmlContent)');
186  $matches = array();
187  preg_match("/<img.*src=[\"'](.*)[\"']/i", $htmlContent, $matches); // Not Coding Standard
188  $url = $matches[1];
189  $matches = array();
190  if (preg_match("/\?fileName\=(.*)/i", $url, $matches) == 1) // Not Coding Standard
191  {
192  $imageFileModel = ImageFileModel::getByFileName($matches[1]);
193  return $imageFileModel;
194  }
195  else
196  {
198  $icon = ZurmoHtml::tag('i', array('class' => 'icon-notice'), '');
199  $message = Zurmo::t('ZurmoModule',
200  'Due to recent improvements in the Zurmo email template builder, you are required ' .
201  'to re-import the image from the external URL. The image currently in use is located ' .
202  'here {url}. You can also select from an existing image in the gallery.',
203  array_merge($params, array('{url}' => $url)));
204  $message = ZurmoHtml::tag('p', array(), $message);
205  return ZurmoHtml::tag('div', array('class' => 'image-legacy-message general-issue-notice'), $icon . $message);
206  }
207  }
208 
209  public static function saveImageFromTemporaryFile($tempFilePath, $name)
210  {
211  $fileContent = new FileContent();
212  $fileContent->content = file_get_contents($tempFilePath);
213  $imageProperties = getimagesize($tempFilePath);
214  $imageFileModel = new ImageFileModel();
215  static::resolveImageName($name, $imageProperties['mime']);
216  $imageFileModel->name = $name;
217  $imageFileModel->size = filesize($tempFilePath);
218  $imageFileModel->type = $imageProperties['mime'];
219  $imageFileModel->width = $imageProperties[0];
220  $imageFileModel->height = $imageProperties[1];
221  $imageFileModel->fileContent = $fileContent;
222  if ($imageFileModel->save())
223  {
224  $imageFileModel->createImageCache();
225  $fileUploadData = array(
226  'id' => $imageFileModel->id,
227  'name' => $imageFileModel->name,
228  'summary' => ImageFileModelUtil::getImageSummary($imageFileModel),
229  'size' => FileModelDisplayUtil::convertSizeToHumanReadableAndGet($imageFileModel->size),
230  'thumbnail_url' => Yii::app()->createAbsoluteUrl('zurmo/imageModel/getThumb',
231  array('fileName' => $imageFileModel->getImageCacheFileName())),
232  'filelink' => Yii::app()->createAbsoluteUrl('zurmo/imageModel/getImage',
233  array('fileName' => $imageFileModel->getImageCacheFileName())),
234  'insert_link' => static::resolveInsertLink($imageFileModel),
235  );
236  }
237  else
238  {
239  $message = Zurmo::t('ZurmoModule', 'Error uploading the image');
240  $fileUploadData = array('error' => $message);
241  }
242  return $fileUploadData;
243  }
244 
245  protected static function resolveImageName(& $name, $mimeType)
246  {
247  if (preg_match("/\..{3,4}\z/i", $name) == 0) // Not Coding Standard
248  {
249  $extension = str_replace('image/', '', $mimeType);
250  $name .= $extension == 'jpeg' ? '.jpg' : '.' . $extension;
251  }
252  }
253 
254  public static function importFromUrl($url)
255  {
256  $tempFilePath = tempnam(sys_get_temp_dir(), 'upload_image_from_url_');
257  $name = preg_replace("#.*\/#", '', $url);
258  file_put_contents($tempFilePath, file_get_contents($url));
259  $fileUploadData = static::saveImageFromTemporaryFile($tempFilePath, $name);
260  return $fileUploadData;
261  }
262  }
263 ?>
static getImageCachePathByFileName($fileName, $shouldReturnThumbnail)
static getTranslationParamsForAllModules()
Definition: LabelUtil.php:75
getImageCachePath($shouldGetThumbnail=false)
static convertSizeToHumanReadableAndGet($size)
createImageCache($shouldCreateThumbnail=false)
static readImageFromCache($fileName, $shouldGetThumbnail=false)
static getMimeType($file, $magicFile=null, $checkExtension=true)
static getByFileName($fileName)
Generated on Fri Aug 7 2020 07:10:35
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.