6 # SPTImage($ImageIdOrFileNameOrImageObj,
7 # $MaxPreviewWidth = NULL, $MaxPreviewHeight = NULL,
8 # $MaxThumbnailWidth = NULL, $MaxThumbnailHeight = NULL)
11 # - delete image and associated files and data
12 # AltText($NewValue = NULL)
13 # - get/set alt text attribute
27 # AUTHOR: Edward Almasy
30 # Copyright 2002-2011 Internet Scout Project
31 # http://scout.wisc.edu
36 # ---- PUBLIC INTERFACE --------------------------------------------------
46 function SPTImage($ImageIdOrFileNameOrImageObj,
47 $MaxWidth = NULL, $MaxHeight = NULL,
48 $MaxPreviewWidth = NULL, $MaxPreviewHeight = NULL,
49 $MaxThumbnailWidth = NULL, $MaxThumbnailHeight = NULL)
51 # clear error status (0 = AI_OKAY)
52 $this->ErrorStatus = 0;
54 # trigger the Image class file to be autoloaded since some parts of this
55 # class (SPTImage) use constants defined in it but don't construct Image
59 # create and save a database handle for our use
62 # if image object was passed in
63 if (is_object($ImageIdOrFileNameOrImageObj)
64 && method_exists($ImageIdOrFileNameOrImageObj,
"SPTImage"))
66 # create copy of image passed in
69 # else if image ID was passed in
70 elseif (($ImageIdOrFileNameOrImageObj > 0)
71 && preg_match(
"/[0-9]+/", $ImageIdOrFileNameOrImageObj))
73 # load info on existing image
76 # else assume that value passed in is file name
79 # create new image from named file
81 $MaxWidth, $MaxHeight,
82 $MaxPreviewWidth, $MaxPreviewHeight,
83 $MaxThumbnailWidth, $MaxThumbnailHeight);
107 # if new value supplied and new value differs from existing value
108 if (($NewValue !== NULL) && ($NewValue != $this->
AltText))
110 # save new value to database
111 $this->DB->Query(
"UPDATE Images SET"
112 .
" AltText = '".addslashes($NewValue).
"'"
113 .
" WHERE ImageId = ".$this->
Id);
115 # save new value locally
119 # return attribute value to caller
123 # delete image and associated files and data
126 # delete base image file
127 if (file_exists($this->FileName)) { unlink($this->FileName); }
129 # delete preview image file
130 if (file_exists($this->PreviewFileName)) { unlink($this->PreviewFileName); }
132 # delete thumbnail image file
133 if (file_exists($this->ThumbnailFileName)) { unlink($this->ThumbnailFileName); }
135 # delete image info record in database
136 $this->DB->Query(
"DELETE FROM Images WHERE ImageId = ".$this->
Id);
139 # return error status set by the constructor
145 # check to make sure image storage directories are available
146 # (returns array of error codes or NULL if no errors found)
150 $ImagePath = self::IMAGE_PATH;
151 $PreviewPath = self::PREVIEW_PATH;
152 $ThumbnailPath = self::THUMBNAIL_PATH;
154 # assume everything will be okay
157 # check base image directory
158 if (!is_dir($ImagePath) || !is_writable($ImagePath))
160 if (!is_dir($ImagePath))
162 @mkdir($ImagePath, 0755);
166 @chmod($ImagePath, 0755);
168 if (!is_dir($ImagePath))
170 $ErrorsFound[] =
"Image Storage Directory Not Found";
172 elseif (!is_writable($ImagePath))
174 $ErrorsFound[] =
"Image Storage Directory Not Writable";
178 # check preview directory
179 if (!is_dir($PreviewPath) || !is_writable($PreviewPath))
181 if (!is_dir($PreviewPath))
183 @mkdir($PreviewPath, 0755);
187 @chmod($PreviewPath, 0755);
189 if (!is_dir($PreviewPath))
191 $ErrorsFound[] =
"Preview Storage Directory Not Found";
193 elseif (!is_writable($PreviewPath))
195 $ErrorsFound[] =
"Preview Storage Directory Not Writable";
199 # check thumbnail directory
200 if (!is_dir($ThumbnailPath) || !is_writable($ThumbnailPath))
202 if (!is_dir($ThumbnailPath))
204 @mkdir($ThumbnailPath, 0755);
208 @chmod($ThumbnailPath, 0755);
210 if (!is_dir($ThumbnailPath))
212 $ErrorsFound[] =
"Thumbnail Storage Directory Not Found";
214 elseif (!is_writable($ThumbnailPath))
216 $ErrorsFound[] =
"Thumbnail Storage Directory Not Writable";
220 # return any errors found to caller
224 public function Resize($MaxWidth, $MaxHeight,
225 $MaxPreviewWidth, $MaxPreviewHeight,
226 $MaxThumbnailWidth, $MaxThumbnailHeight)
228 $SrcImage =
new Image($this->FileName);
230 # scale the original image if necessary
231 $MaxWidth = min($MaxWidth, $SrcImage->XSize());
232 $MaxHeight = min($MaxHeight, $SrcImage->YSize());
233 $SrcImage->ScaleTo($MaxWidth, $MaxHeight, TRUE);
235 # save and reload image info
236 $SrcImage->SaveAs($this->FileName);
237 $SrcImage =
new Image($this->FileName);
239 # retrieve image width and height
240 $this->
Height = $SrcImage->YSize();
241 $this->
Width = $SrcImage->XSize();
243 # generate preview image and calculate width and height
244 $MaxPreviewWidth = min($MaxPreviewWidth, $this->
Width);
245 $MaxPreviewHeight = min($MaxPreviewHeight, $this->
Height);
246 $SrcImage->ScaleTo($MaxPreviewWidth, $MaxPreviewHeight, TRUE);
247 $SrcImage->SaveAs($this->PreviewFileName,
IMGTYPE_JPEG);
248 if (($this->
Width * $MaxPreviewHeight)
249 > ($this->
Height * $MaxPreviewWidth))
253 ($MaxPreviewWidth * $SrcImage->YSize()) / $SrcImage->XSize();
258 ($MaxPreviewHeight * $SrcImage->XSize()) / $SrcImage->YSize();
262 # generate thumbnail image and calculate width and height
263 $MaxThumbnailWidth = min($MaxThumbnailWidth, $this->
Width);
264 $MaxThumbnailHeight = min($MaxThumbnailHeight, $this->
Height);
265 $SrcImage->ScaleTo($MaxThumbnailWidth, $MaxThumbnailHeight, TRUE);
266 $SrcImage->SaveAs($this->ThumbnailFileName,
IMGTYPE_JPEG);
267 if (($this->
Width * $MaxThumbnailHeight)
268 > ($this->
Height * $MaxThumbnailWidth))
272 ($MaxThumbnailWidth * $SrcImage->YSize()) / $SrcImage->XSize();
276 $this->
ThumbnailWidth = ($MaxThumbnailHeight * $SrcImage->XSize()) / $SrcImage->YSize();
280 # save image attributes to database
284 # ---- PRIVATE INTERFACE -------------------------------------------------
305 $MaxPreviewWidth, $MaxPreviewHeight,
306 $MaxThumbnailWidth, $MaxThumbnailHeight)
308 # if file does not exist or is not readable
316 # if image is invalid or unsupported type
318 if ($SrcImage->Status() !=
AI_OKAY)
321 $this->ErrorStatus = $SrcImage->Status();
325 # retrieve image type
326 $this->
Format = $SrcImage->Type();
328 # generate new image ID
331 # generate and set file names
334 # if our image file name differs from file name passed in
335 if (realpath($this->FileName) != realpath(
$FileName))
338 $SrcImage->SaveAs($this->FileName);
340 # if create failed set error status and bail out
341 if ($SrcImage->Status() !=
AI_OKAY)
343 echo
"create failed<br>";
344 echo
"Status: ".$SrcImage->Status().
"<br>";
345 echo
"Failed Command: ".$SrcImage->FailedExternalCommand().
"<br>";
346 echo
"Missing External Executables: ";
349 $this->ErrorStatus = $SrcImage->Status();
354 # scale the original image if necessary
355 $MaxWidth = min($MaxWidth, $SrcImage->XSize());
356 $MaxHeight = min($MaxHeight, $SrcImage->YSize());
357 $SrcImage->ScaleTo($MaxWidth, $MaxHeight, TRUE);
359 # save and reload image info
360 $SrcImage->SaveAs($this->FileName);
361 $SrcImage =
new Image($this->FileName);
363 # retrieve image width and height
364 $this->
Height = $SrcImage->YSize();
365 $this->
Width = $SrcImage->XSize();
367 # generate preview image and calculate width and height
368 $MaxPreviewWidth = min($MaxPreviewWidth, $this->
Width);
369 $MaxPreviewHeight = min($MaxPreviewHeight, $this->
Height);
370 $SrcImage->ScaleTo($MaxPreviewWidth, $MaxPreviewHeight, TRUE);
371 $SrcImage->SaveAs($this->PreviewFileName,
IMGTYPE_JPEG);
372 if ($SrcImage->Status() !=
AI_OKAY)
374 echo
"preview save as failed<br>";
375 $this->ErrorStatus = $SrcImage->Status();
378 if (($this->
Width * $MaxPreviewHeight)
379 > ($this->
Height * $MaxPreviewWidth))
383 ($MaxPreviewWidth * $SrcImage->YSize()) / $SrcImage->XSize();
388 ($MaxPreviewHeight * $SrcImage->XSize()) / $SrcImage->YSize();
392 # generate thumbnail image and calculate width and height
393 $MaxThumbnailWidth = min($MaxThumbnailWidth, $this->
Width);
394 $MaxThumbnailHeight = min($MaxThumbnailHeight, $this->
Height);
395 $SrcImage->ScaleTo($MaxThumbnailWidth, $MaxThumbnailHeight, TRUE);
396 $SrcImage->SaveAs($this->ThumbnailFileName,
IMGTYPE_JPEG);
397 if ($SrcImage->Status() !=
AI_OKAY)
399 echo
"thumbnail SaveAs failed.<br>";
400 $this->ErrorStatus = $SrcImage->Status();
403 if (($this->
Width * $MaxThumbnailHeight)
404 > ($this->
Height * $MaxThumbnailWidth))
408 ($MaxThumbnailWidth * $SrcImage->YSize()) / $SrcImage->XSize();
412 $this->
ThumbnailWidth = ($MaxThumbnailHeight * $SrcImage->XSize()) / $SrcImage->YSize();
416 # save image attributes to database
425 $this->
Id = $ImageId;
427 # load image record from database
428 $this->DB->Query(
"SELECT * FROM Images WHERE ImageId = ".$ImageId);
430 # if the ID is invalid
431 if (!$this->DB->NumRowsSelected())
437 $Record = $this->DB->FetchRow();
439 # load in values from record
440 $this->
Format = $Record[
"Format"];
441 $this->
AltText = $Record[
"AltText"];
442 $this->
Height = $Record[
"Height"];
443 $this->
Width = $Record[
"Width"];
449 # generate file names
455 $Image =
new Image($SrcImage->Url());
456 if ($Image->Status() !=
AI_OKAY)
459 $this->ErrorStatus = $Image->
Status();
463 # generate new image ID
466 # generate file names
469 # copy attributes from source image
470 $this->
Format = $SrcImage->Format();
471 $this->
AltText = $SrcImage->AltText();
472 $this->
Width = $SrcImage->Width();
473 $this->
Height = $SrcImage->Height();
479 # copy source image files
484 # save image attributes to database
488 # generate and save image, preview, and thumnail file names
489 # (requires image ID and format to be set beforehand)
501 $this->FileName = self::IMAGE_PATH .
"Img--"
502 .sprintf(
"%08d.", $this->
Id).$FileExtension;
503 $this->PreviewFileName = self::PREVIEW_PATH .
"Preview--"
504 .sprintf(
"%08d.", $this->
Id).$FileExtension;
505 $this->ThumbnailFileName = self::THUMBNAIL_PATH .
"Thumb--"
506 .sprintf(
"%08d.", $this->
Id).$FileExtension;
509 # retrieve next image ID
512 # look up highest image ID in database
513 $CurrentHighestId = $this->DB->Query(
"SELECT ImageId FROM Images"
514 .
" ORDER BY ImageId DESC LIMIT 1",
517 # return next highest ID or 1 if no ID yet used
518 return ($CurrentHighestId > 0) ? ($CurrentHighestId + 1) : 1;
521 # store image attributes to database
524 # look for existing image record with matching ID
525 $RecordCount = $this->DB->Query(
"SELECT COUNT(*) AS RecordCount FROM Images"
526 .
" WHERE ImageId = ".$this->
Id,
529 # if matching ID found
530 if ($RecordCount > 0)
532 # update existing image record
533 $this->DB->Query(
"UPDATE Images SET"
534 .
" Format = '" .$this->
Format.
"',"
535 .
" AltText = '" .addslashes($this->AltText).
"',"
536 .
" Height = '" .$this->Height.
"',"
537 .
" Width = '" .$this->Width.
"',"
538 .
" PreviewHeight = '" .$this->PreviewHeight.
"',"
539 .
" PreviewWidth = '" .$this->PreviewWidth.
"',"
541 .
" ThumbnailWidth = '" .$this->ThumbnailWidth.
"'"
546 # add new image record
547 $this->DB->Query(
"INSERT INTO Images SET"
548 .
" ImageId = '" .$this->
Id.
"',"
549 .
" Format = '" .$this->Format.
"',"
550 .
" AltText = '" .addslashes($this->AltText).
"',"
551 .
" Height = '" .$this->Height.
"',"
552 .
" Width = '" .$this->Width.
"',"
553 .
" PreviewHeight = '" .$this->PreviewHeight.
"',"
554 .
" PreviewWidth = '" .$this->PreviewWidth.
"',"
556 .
" ThumbnailWidth = '" .$this->ThumbnailWidth.
"'");