PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/services/services/images/gd2.php

https://github.com/jinzora/jinzora3
PHP | 326 lines | 158 code | 54 blank | 114 comment | 39 complexity | 149d324ef23d323a1cf5c147a1163783 MD5 | raw file
  1. <?php if (!defined(JZ_SECURE_ACCESS)) die ('Security breach detected.');
  2. /**
  3. * - JINZORA | Web-based Media Streamer -
  4. *
  5. * Jinzora is a Web-based media streamer, primarily desgined to stream MP3s
  6. * (but can be used for any media file that can stream from HTTP).
  7. * Jinzora can be integrated into a CMS site, run as a standalone application,
  8. * or integrated into any PHP website. It is released under the GNU GPL.
  9. *
  10. * - Resources -
  11. * - Jinzora Author: Ross Carlson <ross@jasbone.com>
  12. * - Web: http://www.jinzora.org
  13. * - Documentation: http://www.jinzora.org/docs
  14. * - Support: http://www.jinzora.org/forum
  15. * - Downloads: http://www.jinzora.org/downloads
  16. * - License: GNU GPL <http://www.gnu.org/copyleft/gpl.html>
  17. *
  18. * - Contributors -
  19. * Please see http://www.jinzora.org/team.html
  20. *
  21. * - Code Purpose -
  22. * - This is the GD2 image resize service
  23. *
  24. * @since 04.05.05
  25. * @author Ben Dodson <ben@jinzora.org>
  26. * @author Ross Carlson <ross@jinzora.org>
  27. */
  28. $jzSERVICE_INFO = array();
  29. $jzSERVICE_INFO['name'] = "GD2 Image Resizing";
  30. $jzSERVICE_INFO['url'] = "http://www.boutell.com/gd/";
  31. define('SERVICE_IMAGES_gd2','true');
  32. /**
  33. * Rotates an image
  34. *
  35. * @author Ross Carlson
  36. * @version 4/16/05
  37. * @since 4/15/05
  38. * @param $image string the FULL path to the image to rotate
  39. */
  40. function SERVICE_ROTATE_IMAGE_gd2($image, $node){
  41. global $allow_filesystem_modify, $include_path;
  42. // Let's make sure they have GD installed
  43. if (gd_version() < 2) { return false; }
  44. // Well since we can't rotate an ID3 image let's see if it's ID3
  45. if (stristr($image,"ID3:")){
  46. // Now let's make a file out of this data
  47. $jzSERVICES = new jzServices();
  48. $jzSERVICES->loadStandardServices();
  49. // Now let's fix the path
  50. $path = substr($image,4);
  51. $meta = $jzSERVICES->getTagData($path);
  52. // Now let's create a file
  53. $file = $include_path. 'data/images/'. str_replace("/","--",$path);
  54. $handle = fopen($file, "wb");
  55. fwrite($handle,$meta['pic_data']);
  56. fclose($handle);
  57. // Now let's make this the image for the node
  58. $node->addMainArt($file);
  59. // Now let's update the name so we can rotate
  60. $image = $file;
  61. }
  62. // First we have to delete any resized versions of this
  63. $files = readDirInfo($include_path. 'data/images', "file");
  64. foreach ($files as $file){
  65. if (stristr($file,str_replace($include_path. 'data/images/',"",substr($image,0,-4)))){
  66. @unlink($file);
  67. }
  68. }
  69. // Now let's set our images
  70. $source = @imagecreatefromjpeg($image);
  71. $rotate = @imagerotate($source, 90, 0);
  72. @imagejpeg($rotate,$image);
  73. }
  74. /**
  75. * Creates resized images for a single image
  76. *
  77. * @author Ross Carlson
  78. * @version 4/05/05
  79. * @since 4/05/05
  80. * @param $image string the FULL path to the image to resize
  81. * @param $dimensions string The size of the image to create
  82. * @return returns true or false if the image was resized properly (boolean)
  83. */
  84. function SERVICE_CREATE_IMAGE_gd2($image, $dimensions, $text, $imageType = "audio", $force_create = "false") {
  85. global $include_path, $create_blank_art;
  86. // Let's make sure they have GD installed
  87. if (gd_version() < 2) { return false; }
  88. // Now should we just leave?
  89. if ($create_blank_art == "false" and $force_create == "false"){ return false; }
  90. // First let's figure out the name for the image
  91. $iArr = explode("/",$image);
  92. $imgName = $iArr[count($iArr)-1];
  93. unset($iArr[count($iArr)-1]);
  94. $name = substr(implode("--",$iArr),2);
  95. $path = $include_path. "data/images/". $name;
  96. $image = $path. md5($imgName. $dimensions). ".jpg";
  97. // Let's get the dimensions
  98. $dest_width = 200;
  99. $dest_height = 200;
  100. // Let's setup some things for below
  101. $drop = 0;
  102. $shadow = 0;
  103. $font = 5;
  104. $maxwidth = 200;
  105. $truncate = 38;
  106. // Now let's see if we're too long...
  107. if (strlen($text) > $truncate + 3){$text = substr($text,0,$truncate). "...";}
  108. // Ok, we now have the path let's create it
  109. switch ($imageType){
  110. case "audio":
  111. $origImageName = "default.jpg";
  112. break;
  113. case "video":
  114. $origImageName = "video.jpg";
  115. break;
  116. }
  117. $src_img = imagecreatefromjpeg($include_path. "style/images/". $origImageName);
  118. $dest_img = imageCreateTrueColor($dest_width, $dest_height);
  119. // decode color arguments and allocate colors
  120. $color = imagecolorallocate($dest_img, 255, 255, 255);
  121. $shadow = imagecolorallocate($dest_img, 0, 0, 0);
  122. // Let's get the width and height of the source image
  123. $src_width = imagesx($src_img);
  124. $src_height = imagesy($src_img);
  125. /* Now let's copy the data from the old picture to the new one witht the new settings */
  126. imageCopyResampled($dest_img, $src_img, 0, 0, 0 ,0, $dest_width, $dest_height, $src_width, $src_height);
  127. // Now let's clean up our temp image
  128. imagedestroy($src_img);
  129. // Let's setup the font
  130. $fontwidth = ImageFontWidth(5);
  131. $fontheight = ImageFontHeight(5);
  132. // So that shadow is not off image on right align & bottom valign
  133. $margin = floor(5 + 0)/2;
  134. if ($maxwidth != NULL) {
  135. $maxcharsperline = floor( ($dest_width - ($margin * 2)) / $fontwidth);
  136. $text = wordwrap($text, $maxcharsperline, "\n", 1);
  137. }
  138. $lines = explode("\n", $text);
  139. // Now let's setup the alignment
  140. $y = ((imageSY($dest_img) - ($fontheight * sizeof($lines)))/2) + 50;
  141. while (list($numl, $line) = each($lines)) {
  142. ImageString($dest_img, $font, floor((imagesx($dest_img) - $fontwidth*strlen($line))/2)+$drop, ($y+$drop), $line, $shadow);
  143. ImageString($dest_img, $font, floor((imagesx($dest_img) - $fontwidth*strlen($line))/2), $y, $line, $color);
  144. $y += $fontheight;
  145. }
  146. // Now let's create our new image
  147. @touch($image);
  148. // Now let's make sure that new image is writable
  149. if (is_writable($image)){
  150. // Let's create it
  151. imagejpeg($dest_img, $image);
  152. // Now let's clean up our temp image
  153. imagedestroy($dest_img);
  154. // Now we need to resize this
  155. return SERVICE_RESIZE_IMAGE_gd2($image,$dimensions);
  156. } else {
  157. return false;
  158. }
  159. }
  160. /**
  161. * Creates resized images for a single image
  162. *
  163. * @author Ross Carlson
  164. * @version 4/05/05
  165. * @since 4/05/05
  166. * @param $image string the FULL path to the image to resize
  167. * @param $dimensions string The size of the image to create
  168. * @return the name of the resized image if resize succeeded, or the original filename if it failed
  169. */
  170. function SERVICE_RESIZE_IMAGE_gd2($image, $dimensions, $dest = false, $imageType = "audio") {
  171. global $include_path, $keep_porportions, $resize_images;
  172. // Let's make sure they have GD installed
  173. if (gd_version() < 2) { return $image; }
  174. // Now let's make sure this is a JPG
  175. #if (!stristr($image,".jpg") and !stristr($image,"ID3:")){return $image;}
  176. if ( !(stristr($image,".jpg") || stristr($image, ".gif") ) and !stristr($image,"ID3:")){return $image;}
  177. // Should we do this at all?
  178. if ($resize_images == "false"){return $image;}
  179. // Ok, let's get on with it...
  180. // First let's create our filenames
  181. $iArr = explode("/",$image);
  182. $imgName = $iArr[count($iArr)-1];
  183. // Now let's see where this is gonna go
  184. unset($iArr[count($iArr)-1]);
  185. unset($iArr[0]);
  186. $name = implode("--",$iArr);
  187. $path = $name. "--";
  188. if ($dest){
  189. $destImage = $dest;
  190. } else {
  191. $destImage = $path. str_replace(".jpg","",$imgName);
  192. $destImage = "data/images/". md5($destImage). ".". $dimensions. ".jpg";
  193. }
  194. // Now let's create the images IF they don't exist
  195. if (!is_file($destImage)){
  196. return createImage($image,$destImage,$dimensions);
  197. } else {
  198. return $destImage;
  199. }
  200. }
  201. function createImage($source, $destination, $dimensions){
  202. global $keep_porportions, $include_path, $jzSERVICES;
  203. // Ok, let's make sure the dimensions aren't empty
  204. if (!$dimensions){return;}
  205. // Ok, first we need to see if this is an ID3 image and if so write it to a temp file
  206. if (strstr($source,"ID3:")){
  207. // Now let's get the data from the ID3 tag so we can write it out
  208. $path = substr($source,4);
  209. $meta = $jzSERVICES->getTagData($path);
  210. // Now let's create the new image file
  211. $source = $include_path. "temp/tempimage.jpg";
  212. $handle = fopen($source, "wb");
  213. fwrite($handle,$meta['pic_data']);
  214. fclose($handle);
  215. if (sizeof($destination)>=5){
  216. $destination = substr($destination,0,-4). ".". $dimensions. ".jpg";
  217. } else {
  218. $destination = substr($destination,0). ".". $dimensions. ".jpg";
  219. }
  220. }
  221. // Let's grab the source image to work with it
  222. if (($src_img = @imagecreatefromjpeg($source)) == false){
  223. $gdInfo = gd_info();
  224. $gdGifSupport = $gdInfo["GIF Read Support"];
  225. if( !$gdGifSupport ) { return $source; }
  226. if (($src_img = @imagecreatefromgif($source)) == false){
  227. // We couldn't resize, so let's just return the original image
  228. return $source;
  229. }
  230. }
  231. if ($src_img <> ""){
  232. // Let's get the width and height of the source image
  233. $src_width = imagesx($src_img);
  234. $src_height = imagesy($src_img);
  235. // Let's set the width and height of the new image we'll create
  236. $destArr = explode("x",$dimensions);
  237. $dest_width = $destArr[0];
  238. $dest_height = $destArr[1];
  239. // Now if the picture isn't a standard resolution (like 640x480) we
  240. // need to find out what the new image size will be by figuring
  241. // out which of the two numbers is higher and using that as the scale
  242. // First let's make sure they wanted to keep the porportions or not
  243. if ($keep_porportions == "true"){
  244. if ($src_width > $src_height){
  245. /* ok so the width is the bigger number so the width doesn't change
  246. We need to figure out the percent of change by dividing the source width
  247. by the dest width */
  248. $scale = $src_width / $dest_width;
  249. $dest_height = $src_height / $scale;
  250. } else {
  251. /* ok so the width is the bigger number so the width doesn't change
  252. We need to figure out the percent of change by dividing the source width
  253. by the dest width */
  254. $scale = $src_height / $dest_height;
  255. $dest_width = $src_width / $scale;
  256. }
  257. }
  258. // Now let's create our destination image with our new height/width
  259. $dest_img = imageCreateTrueColor($dest_width, $dest_height);
  260. // Now let's copy the data from the old picture to the new one witht the new settings
  261. imageCopyResampled($dest_img, $src_img, 0, 0, 0 ,0, $dest_width, $dest_height, $src_width, $src_height);
  262. // Now let's create our new image
  263. imagejpeg($dest_img, $include_path. $destination);
  264. // Now let's clean up all our temp images
  265. //imagedestroy($src_img);
  266. //imagedestroy($dest_img);
  267. return $destination;
  268. } else {
  269. return false;
  270. }
  271. }
  272. ?>