PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/script/lib/PHP/Depend/Util/ImageConvert.php

https://bitbucket.org/renaatdemuynck/chamilo
PHP | 207 lines | 84 code | 25 blank | 98 comment | 11 complexity | 37bfd9296cfecb3f81ab9ff1ff21cbed MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT, GPL-2.0
  1. <?php
  2. /**
  3. * This file is part of PHP_Depend.
  4. *
  5. * PHP Version 5
  6. *
  7. * Copyright (c) 2008-2010, Manuel Pichler <mapi@pdepend.org>.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * * Neither the name of Manuel Pichler nor the names of his
  23. * contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  30. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  32. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * @category QualityAssurance
  40. * @package PHP_Depend
  41. * @subpackage Util
  42. * @author Manuel Pichler <mapi@pdepend.org>
  43. * @copyright 2008-2010 Manuel Pichler. All rights reserved.
  44. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  45. * @version SVN: $Id$
  46. * @link http://pdepend.org/
  47. */
  48. require_once 'PHP/Depend/Util/ConfigurationInstance.php';
  49. /**
  50. * Simple utility class that is used to create different image formats. This
  51. * class can use the ImageMagick cli tool <b>convert</b> and the pecl extension
  52. * <b>pecl/imagick</b>.
  53. *
  54. * @category QualityAssurance
  55. * @package PHP_Depend
  56. * @subpackage Util
  57. * @author Manuel Pichler <mapi@pdepend.org>
  58. * @copyright 2008-2010 Manuel Pichler. All rights reserved.
  59. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  60. * @version Release: 0.9.19
  61. * @link http://pdepend.org/
  62. */
  63. class PHP_Depend_Util_ImageConvert
  64. {
  65. /**
  66. * Tries to converts the <b>$input</b> image into the <b>$output</b> format.
  67. *
  68. * @param string $input The input file.
  69. * @param string $output The output file.
  70. *
  71. * @return void
  72. */
  73. public static function convert($input, $output)
  74. {
  75. $inputType = strtolower(pathinfo($input, PATHINFO_EXTENSION));
  76. $outputType = strtolower(pathinfo($output, PATHINFO_EXTENSION));
  77. // Check for output file without extension and reuse input type
  78. if ($outputType === '')
  79. {
  80. $outputType = $inputType;
  81. $output .= ".{$outputType}";
  82. }
  83. if ($inputType === 'svg')
  84. {
  85. self :: prepareSVG($input);
  86. }
  87. if ($inputType === $outputType)
  88. {
  89. file_put_contents($output, file_get_contents($input));
  90. }
  91. else
  92. if (extension_loaded('imagick') === true)
  93. {
  94. $im = new Imagick($input);
  95. $im->setImageFormat($outputType);
  96. $im->writeImage($output);
  97. // The following code is not testable when imagick is installed
  98. // @codeCoverageIgnoreStart
  99. }
  100. else
  101. if (self :: hasImagickConvert() === true)
  102. {
  103. $input = escapeshellarg($input);
  104. $output = escapeshellarg($output);
  105. system("convert {$input} {$output}");
  106. }
  107. else
  108. {
  109. $fallback = substr($output, 0, - strlen($outputType)) . $inputType;
  110. echo "WARNING: Cannot generate image of type '{$outputType}'. This", " feature needs either the\n pecl/imagick extension or", " the ImageMagick cli tool 'convert'.\n\n", "Writing alternative image:\n{$fallback}\n\n";
  111. file_put_contents($fallback, file_get_contents($input));
  112. }
  113. // @codeCoverageIgnoreEnd
  114. }
  115. /**
  116. * Tests that the ImageMagick CLI tool <b>convert</b> exists.
  117. *
  118. * @return boolean
  119. */
  120. protected static function hasImagickConvert()
  121. {
  122. // @codeCoverageIgnoreStart
  123. $desc = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'a'));
  124. $proc = proc_open('convert', $desc, $pipes);
  125. if (is_resource($proc))
  126. {
  127. fwrite($pipes[0], '-version');
  128. fclose($pipes[0]);
  129. return (0 === proc_close($proc));
  130. }
  131. return false;
  132. // @codeCoverageIgnoreEnd
  133. }
  134. /**
  135. * Utility method for svg input files.
  136. *
  137. * If the input file has the mime type svg and a configuration file with
  138. * imageConvert options exists, this method will prepare the input image
  139. * file.
  140. *
  141. * @param string $input The input svg file.
  142. *
  143. * @return void
  144. */
  145. protected static function prepareSVG($input)
  146. {
  147. // Check for a configuration instance
  148. if (($config = PHP_Depend_Util_ConfigurationInstance :: get()) === null)
  149. {
  150. return;
  151. }
  152. $svg = file_get_contents($input);
  153. // Check for font family
  154. if (isset($config->imageConvert->fontFamily))
  155. {
  156. // Get font family
  157. $fontFamily = (string) $config->imageConvert->fontFamily;
  158. // Replace CSS separators
  159. $fontReplace = 'font-family:' . strtr($fontFamily, ';:', ' ');
  160. $fontPattern = '/font-family:\s*Arial/';
  161. $svg = preg_replace($fontPattern, $fontReplace, $svg);
  162. }
  163. // Check for font size
  164. if (isset($config->imageConvert->fontSize))
  165. {
  166. // Get font size
  167. $fontSize = abs((float) $config->imageConvert->fontSize);
  168. // Fetch all font-size expressions
  169. preg_match_all('/font-size:\s*(\d+)/', $svg, $fontSizes);
  170. $fontSizes = array_unique($fontSizes[1]);
  171. $resize = ($fontSize - max($fontSizes));
  172. foreach ($fontSizes as $fontSize)
  173. {
  174. // Calculate resize value
  175. $fontReplace = 'font-size:' . ($fontSize + $resize);
  176. $fontPattern = "/font-size:\s*{$fontSize}/";
  177. $svg = preg_replace($fontPattern, $fontReplace, $svg);
  178. }
  179. }
  180. file_put_contents($input, $svg);
  181. }
  182. }