Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / web / public_php / api / server / item_icon.php
blob145cc6e6242363c827c868cfd445e93f8edf9123
1 <?php
3 /* Copyright (C) 2009 Winch Gate Property Limited
5 * This file is part of ryzom_api.
6 * ryzom_api is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * ryzom_api is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with ryzom_api. If not, see <http://www.gnu.org/licenses/>.
20 require_once(RYAPI_PATH.'data/ryzom/sbrick_db.php');
21 require_once(RYAPI_PATH.'data/ryzom/items_db.php');
23 $ryzom_item_icon_path = RYAPI_PATH.'data/ryzom/interface/';
25 // return TRUE if $bg was modified, FALSE if not
26 // ryzom_item_icon_colorize if needed, apply mask if needed and mask color is set
27 function ryzom_item_icon_load_image($bg, $fileName, $color=-1, $mColor=-1){
28 if(!file_exists($fileName) || is_dir($fileName)){
29 // requested fileName not found or it points to directory
30 return false;
32 $im = @imagecreatefrompng($fileName);
33 if($im===false){
34 return false;
36 imagesavealpha($im, true);
38 $im_w=imagesx($im);
39 $im_h=imagesy($im);
41 $bg_w=imagesx($bg);
42 $bg_h=imagesy($bg);
44 if($color != -1) {
45 $im=ryzom_item_icon_colorize($im, ($color >> 24) & 255, $color&255, ($color>>8)&255, ($color >> 16)&255);
46 }elseif(/*$rzColor*/1 != -1){
47 $mask_file=str_replace('.png', '_mask.png', $fileName);
48 if(file_exists($mask_file)){
49 $mask = imagecreatefrompng($mask_file);
50 if($mask!==false){
51 imagesavealpha($mask, true);
52 $mask=ryzom_item_icon_colorize($mask, ($mColor >> 24) & 255, $mColor&255, ($mColor>>8)&255, ($mColor >> 16)&255);
53 imagecopy($im, $mask, 0, 0, 0, 0, $im_w, $im_h);
54 imagedestroy($mask);
58 if($bg_w<$im_w){
59 // resize source image to background image (make it smaller)
60 imagecopyresized($bg, $im, 0, 0, 0, 0, $bg_w, $bg_h, $im_w, $im_h);
61 }else{
62 // center to background, but do not resize
63 $pad_x=($bg_w-$im_w)/2;
64 $pad_y=($bg_h-$im_h)/2;
65 imagecopy($bg, $im, $pad_x, $pad_y, 0, 0, $im_w, $im_h);
67 imagedestroy($im);
69 return true;
72 function ryzom_item_icon_colorize($im, $ca, $cr, $cg, $cb){
73 $w=imagesx($im);
74 $h=imagesy($im);
76 $out = imagecreatetruecolor($w, $h);
78 imagealphablending($out, false);
80 $black=imagecolorallocate($out, 0, 0, 0);
81 imagefill($out, 0, 0, $black);
83 for($y=0;$y<$h;$y++){
84 for($x=0;$x<$w;$x++){
85 $rgba= imagecolorat($im, $x, $y);
86 $sa = ($rgba>>24)&255;
87 $sr = ($rgba>>16)&255;
88 $sg = ($rgba>>8)&255;
89 $sb = ($rgba>>0)&255;
91 $r=$cr*$sr/255;
92 $g=$cg*$sg/255;
93 $b=$cb*$sb/255;
94 $c=imagecolorallocatealpha($out, $r, $g, $b, $sa);
95 imagesetpixel($out, $x, $y, $c);
99 return $out;
102 function ryzom_item_icon_image_typo_width($txt){
103 global $ryzom_item_icon_path;
104 $x=0;
105 for($i=0;$i<strlen($txt);$i++){
106 if($txt{$i}=='?') $t='question'; else $t=strtolower(substr($txt, $i, 1));
108 $typo_file=$ryzom_item_icon_path.'typo/typo_'.$t.'.png';
110 if(file_exists($typo_file)){
111 $wh=getimagesize($typo_file);
112 if($wh!==false){
113 $x+=$wh[0];
116 }//for
117 return $x;
120 function ryzom_item_icon_image_typo($bg, $txt, $x, $y, $use_numbers=true) {
121 global $ryzom_item_icon_path;
123 for($i=0;$i<strlen($txt);$i++) {
124 if($txt{$i}=='?') $t='question'; else $t=strtolower(substr($txt, $i, 1));
125 // use_numbers is used for sap/stack/quality display
126 // sbrick uses numbers from type_X.png files
127 if($use_numbers && is_numeric($t)){
128 $typo_file=$ryzom_item_icon_path.'typo/numbers_'.$t.'.png';
129 }else{
130 $typo_file=$ryzom_item_icon_path.'typo/typo_'.$t.'.png';
132 if(file_exists($typo_file)){
133 $im=imagecreatefrompng($typo_file);
134 $w=imagesx($im);
135 $h=imagesy($im);
136 imagecopy($bg, $im, $x, $y+(8-$h)/2, 0, 0, $w, $h); // center 'char' to 5x8
137 $x += $w;
142 function split_fname($fname){
143 // get image names and possible colors
144 if(strpos($fname, '|')===false){
145 return array($fname, -1);
146 }else{
147 return explode('|', $fname);
151 function ryzom_item_icon_generate($filename, $sheetid, $c, $q, $s, $sap, $destroyed, $label) {
152 global $ryzom_item_icon_path, $itemsList, $sbrickList;
154 $item = array();
156 if(preg_match('/\.sbrick$/', $sheetid)){
157 // variables with different meaning
158 // $q - TRUE - show sbrick level
159 // $s - custom text on bottom-left corner
160 $item = $sbrickList[$sheetid];
161 $w=24; $h=24;
162 }else{
163 if (array_key_exists($sheetid, $itemsList))
164 $item = $itemsList[$sheetid];
165 else
166 $item = $itemsList['test_scroll.sitem'];
168 $w=40; $h=40;
171 $color_array = array(
172 0 => array(233,22,0,255),
173 1 => array(220,140,50,255),
174 2 => array(170,250,0,255),
175 3 => array(0,215,120,255),
176 4 => array(50,100,255,255),
177 5 => array(170,55,110,255),
178 6 => array(250,250,250,255),
179 7 => array(80,80,120,255),
181 $mask_color = -1;
182 if(isset($color_array[$c])) {
183 $mask_color = $color_array[$c][0] + ($color_array[$c][1] << 8) + ($color_array[$c][2] << 16);
186 list($bgFile, $bgColor) = split_fname($item['bg']);
187 list($fgFile, $fgColor) = split_fname($item['fg']);
188 list($i1File, $i1Color) = split_fname($item['i1']);
189 list($i2File, $i2Color) = split_fname($item['i2']);
191 // background image
192 $bg=imagecreatetruecolor($w, $h);
193 $transp=imagecolorallocatealpha($bg, 0, 0, 0, 127);
194 imagefill($bg, 0, 0, $transp);
195 imagesavealpha($bg, true);
197 // stack item images on-top of background
198 if(ryzom_item_icon_load_image($bg, $ryzom_item_icon_path.$bgFile, $bgColor, $mask_color)){
199 // draw background twice making semi-transparent background darker,
200 // but still allowing icons with full transparent backgrounds like ixpca01.sitem (XP crystals)
201 ryzom_item_icon_load_image($bg, $ryzom_item_icon_path.$bgFile, $bgColor, $mask_color);
203 ryzom_item_icon_load_image($bg, $ryzom_item_icon_path.$fgFile, $fgColor, $mask_color);
204 ryzom_item_icon_load_image($bg, $ryzom_item_icon_path.$i1File, $i1Color, $mask_color);
205 ryzom_item_icon_load_image($bg, $ryzom_item_icon_path.$i2File, $i2Color);
207 if($w==24){// sbrick 24x24
208 if($q===true){ // sbrick level, bottom-right corner
209 $txt_w=ryzom_item_icon_image_typo_width($item['lvl']);
210 ryzom_item_icon_image_typo($bg, $item['lvl'], $w-$txt_w-1, $h-8, false);
212 if(!empty($s)){
213 ryzom_item_icon_image_typo($bg, $s, 1, $h-7, false); // 1px lower than lvl number
215 }else{ // item, 40x40
216 // put item name, quality, stack size and sap loar to final image
217 if($label && $item['txt']!=''){ // top right
218 ryzom_item_icon_image_typo($bg, $item['txt'], 1, 0, false);
220 if($q>0){ // to bottom-right corner
221 ryzom_item_icon_image_typo($bg, $q, 40-strlen($q)*5-1, 32-1, true);// 40-8
223 if($s>0){ // bottom-left corner
224 ryzom_item_icon_image_typo($bg, 'x'.$s, 1, 32-1, true);// 40-8
226 if($sap>=0){ // top-left corner
227 $im=ryzom_item_icon_load_image($bg, $ryzom_item_icon_path.'sapload.png');
228 if($im!==false && $sap>=1){
229 ryzom_item_icon_image_typo($bg, $sap-1, 1, 2, true);
232 if($destroyed){
233 ryzom_item_icon_load_image($bg, $ryzom_item_icon_path.'ico_task_failed.png');
237 imagepng($bg, $filename);
238 return $filename;
241 function ryzom_item_icon_path($sheetid, $c, $q, $s, $sap, $destroyed, $label) {
242 return RYAPI_PATH.'data/cache/item_icons/'.md5($sheetid.$c.$q.$s.$sap.$destroyed.$label).'.png';
245 function ryzom_item_icon($sheetid, $c, $q, $s, $sap, $destroyed, $label) {
246 global $use_cache;
248 $filename = ryzom_item_icon_path($sheetid, $c, $q, $s, $sap, $destroyed, $label);
250 // always use cache
251 if(/*!$use_cache ||*/ !file_exists($filename)) {
252 ryzom_item_icon_generate($filename, $sheetid, $c, $q, $s, $sap, $destroyed, $label);
254 return file_get_contents($filename);
257 function ryzom_item_icon_url($sheetid, $c=1, $q=0, $s=0, $sap=-1, $destroyed=false, $label='') {
258 $filename = ryzom_item_icon_path($sheetid, $c, $q, $s, $sap, $destroyed, $label);
259 if (!file_exists($filename))
260 ryzom_item_icon_generate($filename, $sheetid, $c, $q, $s, $sap, $destroyed, $label);
261 return RYAPI_URL.'data/cache/item_icons/'.md5($sheetid.$c.$q.$s.$sap.$destroyed.$label).'.png';