| 1 | <?php |
|---|
| 2 | /******************************************************************************/ |
|---|
| 3 | // // |
|---|
| 4 | // InstantCMS v1.8 // |
|---|
| 5 | // http://www.instantcms.ru/ // |
|---|
| 6 | // // |
|---|
| 7 | // written by InstantCMS Team, 2007-2010 // |
|---|
| 8 | // produced by InstantSoft, (www.instantsoft.ru) // |
|---|
| 9 | // // |
|---|
| 10 | // LICENSED BY GNU/GPL v2 // |
|---|
| 11 | // // |
|---|
| 12 | /******************************************************************************/ |
|---|
| 13 | |
|---|
| 14 | if(!defined('VALID_CMS')) { die('ACCESS DENIED'); } |
|---|
| 15 | |
|---|
| 16 | function img_add_watermark($src){ |
|---|
| 17 | $size = getimagesize($src); |
|---|
| 18 | |
|---|
| 19 | if ($size === false) return false; |
|---|
| 20 | |
|---|
| 21 | $format = strtolower(substr($size['mime'], strpos($size['mime'], '/')+1)); |
|---|
| 22 | $icfunc = "imagecreatefrom" . $format; |
|---|
| 23 | if (!function_exists($icfunc)) return false; |
|---|
| 24 | |
|---|
| 25 | $isrc = $icfunc($src); |
|---|
| 26 | |
|---|
| 27 | img_watermark($isrc, $size[0], $size[1]); |
|---|
| 28 | |
|---|
| 29 | // âûâîä êàðòèíêè è î÷èñòêà ïàìÿòè |
|---|
| 30 | imagejpeg($isrc,$src,80); |
|---|
| 31 | |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | function img_watermark(&$img, $w, $h){ |
|---|
| 35 | |
|---|
| 36 | $inConf = cmsConfig::getInstance(); |
|---|
| 37 | |
|---|
| 38 | if (!$inConf->wmark) { return; } |
|---|
| 39 | |
|---|
| 40 | $wm_file = $_SERVER['DOCUMENT_ROOT'].'/images/'.$inConf->wmark; |
|---|
| 41 | |
|---|
| 42 | if (!file_exists($wm_file)) { return; } |
|---|
| 43 | |
|---|
| 44 | $size = getimagesize($wm_file); |
|---|
| 45 | |
|---|
| 46 | $wm = imagecreatefrompng($wm_file); |
|---|
| 47 | |
|---|
| 48 | $wm_w = $size[0]; |
|---|
| 49 | $wm_h = $size[1]; |
|---|
| 50 | |
|---|
| 51 | $wm_x = $w - $wm_w; |
|---|
| 52 | $wm_y = $h - $wm_h; |
|---|
| 53 | |
|---|
| 54 | imagecopyresampled($img, $wm, $wm_x, $wm_y, 0, 0, $wm_w, $wm_h, $wm_w, $wm_h); |
|---|
| 55 | |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | /*********************************************************************************** |
|---|
| 59 | Ôóíêöèÿ img_resize(): ãåíåðàöèÿ thumbnails |
|---|
| 60 | Ïàðàìåòðû: |
|---|
| 61 | $src - èìÿ èñõîäíîãî ôàéëà |
|---|
| 62 | $dest - èìÿ ãåíåðèðóåìîãî ôàéëà |
|---|
| 63 | $width, $height - øèðèíà è âûñîòà ãåíåðèðóåìîãî èçîáðàæåíèÿ, â ïèêñåëÿõ |
|---|
| 64 | Íåîáÿçàòåëüíûå ïàðàìåòðû: |
|---|
| 65 | $rgb - öâåò ôîíà, ïî óìîë÷àíèþ - áåëûé |
|---|
| 66 | $quality - êà÷åñòâî ãåíåðèðóåìîãî JPEG, ïî óìîë÷àíèþ - ìàêñèìàëüíîå (100) |
|---|
| 67 | ***********************************************************************************/ |
|---|
| 68 | function img_resize($src, $dest, $maxwidth, $maxheight=160, $is_square=false, $watermark=false, $rgb=0xFFFFFF, $quality=80) |
|---|
| 69 | { |
|---|
| 70 | if (!file_exists($src)) return false; |
|---|
| 71 | |
|---|
| 72 | $upload_dir = dirname($dest); |
|---|
| 73 | if (!is_writable($upload_dir)){ @chmod($dest, 0755); } |
|---|
| 74 | |
|---|
| 75 | $size = getimagesize($src); |
|---|
| 76 | |
|---|
| 77 | if ($size === false) return false; |
|---|
| 78 | |
|---|
| 79 | $new_width = $size[0]; |
|---|
| 80 | $new_height = $size[1]; |
|---|
| 81 | |
|---|
| 82 | if (($new_height <= $maxheight) && ($new_width <= $maxwidth)){ |
|---|
| 83 | @copy($src, $dest); |
|---|
| 84 | return true; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | // Îïðåäåëÿåì èñõîäíûé ôîðìàò ïî MIME-èíôîðìàöèè, ïðåäîñòàâëåííîé |
|---|
| 88 | // ôóíêöèåé getimagesize, è âûáèðàåì ñîîòâåòñòâóþùóþ ôîðìàòó |
|---|
| 89 | // imagecreatefrom-ôóíêöèþ. |
|---|
| 90 | $format = strtolower(substr($size['mime'], strpos($size['mime'], '/')+1)); |
|---|
| 91 | $icfunc = "imagecreatefrom" . $format; |
|---|
| 92 | if (!function_exists($icfunc)) return false; |
|---|
| 93 | |
|---|
| 94 | $isrc = $icfunc($src); |
|---|
| 95 | |
|---|
| 96 | if($is_square){ |
|---|
| 97 | $idest = imagecreatetruecolor($maxwidth,$maxwidth); |
|---|
| 98 | imagefill($idest, 0, 0, $rgb); |
|---|
| 99 | // âûðåçàåì êâàäðàòíóþ ñåðåäèíêó ïî x, åñëè ôîòî ãîðèçîíòàëüíîå |
|---|
| 100 | if ($new_width>$new_height) |
|---|
| 101 | imagecopyresampled($idest, $isrc, 0, 0, round((max($new_width,$new_height)-min($new_width,$new_height))/2), 0, $maxwidth, $maxwidth, min($new_width,$new_height), min($new_width,$new_height)); |
|---|
| 102 | // âûðåçàåì êâàäðàòíóþ âåðõóøêó ïî y, |
|---|
| 103 | if ($new_width<$new_height) |
|---|
| 104 | imagecopyresampled($idest, $isrc, 0, 0, 0, 0, $maxwidth, $maxwidth, min($new_width,$new_height), min($new_width,$new_height)); |
|---|
| 105 | // êâàäðàòíàÿ êàðòèíêà ìàñøòàáèðóåòñÿ áåç âûðåçîê |
|---|
| 106 | if ($new_width==$new_height) |
|---|
| 107 | imagecopyresampled($idest, $isrc, 0, 0, 0, 0, $maxwidth, $maxwidth, $new_width, $new_width); |
|---|
| 108 | } else { |
|---|
| 109 | while ($new_width>$maxwidth) { $new_width *= 0.99; $new_height *= 0.99; } |
|---|
| 110 | while ($new_height>$maxheight) { $new_width *= 0.99; $new_height *= 0.99; } |
|---|
| 111 | $idest = imagecreatetruecolor($new_width, $new_height); |
|---|
| 112 | imagefill($idest, 0, 0, $rgb); |
|---|
| 113 | imagecopyresampled($idest, $isrc, 0, 0, 0, 0, $new_width, $new_height, $size[0], $size[1]); |
|---|
| 114 | |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | if ($watermark) { img_watermark($idest, $new_width, $new_height); } |
|---|
| 118 | |
|---|
| 119 | imageinterlace($idest,1); |
|---|
| 120 | |
|---|
| 121 | // âûâîä êàðòèíêè è î÷èñòêà ïàìÿòè |
|---|
| 122 | imagejpeg($idest,$dest,$quality); |
|---|
| 123 | |
|---|
| 124 | imagedestroy($isrc); |
|---|
| 125 | imagedestroy($idest); |
|---|
| 126 | return true; |
|---|
| 127 | } |
|---|
| 128 | ?> |
|---|