Image.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
12 
17 
18 /**
19  * Image validates an image value.
20  *
21  * Configuration examples:
22  * @code
23  * // width exactly 200px, height less than 100px
24  * image:{"width":[200,1],"height":[100,0]}
25  *
26  * // arbitrary width, height exactly 300px
27  * image:{"height":[300,0]}
28  * @endcode
29  *
30  * @author ingo herwig <ingo@wemove.com>
31  */
32 class Image implements ValidateType {
33 
34  /**
35  * @see ValidateType::validate
36  * $options is an associative array with keys 'width' (optional) and 'height' (optional)
37  */
38  public function validate($value, Message $message, $options=null) {
39  if (strlen($value) == 0) {
40  return true;
41  }
42 
43  $imgWidth = isset($options['width']) ? $options['width'] : false;
44  $imgHeight = isset($options['height']) ? $options['height'] : false;
45 
46  if ($imgWidth === false && $imgHeight === false) {
47  return true;
48  }
49 
50  // translate path
51  $fileUtil = new FileUtil();
52  $absValue = WCMF_BASE.$value;
53  $value = $fileUtil->realpath(dirname($absValue)).'/'.basename($absValue);
54 
55  $graphicsUtil = new GraphicsUtil();
56  if (!$graphicsUtil->isImage($value)) {
57  return false;
58  }
59 
60  // check dimensions of the image
61  if ($imgWidth !== false) {
62  $checkWidth = $graphicsUtil->isValidImageWidth($value, $imgWidth[0], $imgWidth[1]);
63  }
64  else {
65  $checkWidth = true;
66  }
67  if ($imgHeight !== false) {
68  $checkHeight = $graphicsUtil->isValidImageHeight($value, $imgHeight[0], $imgHeight[1]);
69  }
70  else {
71  $checkHeight = true;
72  }
73  if(!($checkWidth && $checkHeight)) {
74  return false;
75  }
76  return true;
77  }
78 }
79 ?>
ValidateType defines the interface for all validator classes.
Message is used to get localized messages to be used in the user interface.
Definition: Message.php:23
Image validates an image value.
Definition: Image.php:32
validate($value, Message $message, $options=null)
Definition: Image.php:38
GraphicsUtil provides support for graphic manipulation.
FileUtil provides basic support for file functionality like HTTP file upload.
Definition: FileUtil.php:22