001 <html> 002 <body> 003 <?php 004 $SilentFlag = 0; 005 $ForceFlag = 0; 006 007 $BlogBasePath = "./"; 008 $ThumbnailDataPath = "image/thumbnail.dat"; 009 010 $ThumbnailWidth = 80; 011 $ThumbnailHeight = 60; 012 013 $DBHost = "(database-hostname)"; 014 $DBUser = "(database-username)"; 015 $DBPassword = "(database-password)"; 016 $Database = "(database-name)"; 017 018 // Set arguments. 019 if(isset($_GET['silent'])) { 020 if($_GET['silent'] <> 0) { 021 $SilentFlag = 1; 022 } 023 } 024 if(isset($_GET['force'])) { 025 if($_GET['force'] <> 0) { 026 $ForceFlag = 1; 027 if(!$SilentFlag) { 028 print("<div>force to create thumbnails.</div>\n"); 029 } 030 } 031 } 032 033 // Read from ThumbnailDataFile to set ThumbnailImageArray. 034 if(!$ThumbnailDataHandle = fopen($BlogBasePath . $ThumbnailDataPath, "r")) { 035 die("fopen(ERROR): $BlogBasePath$ThumbnailDataPath"); 036 } 037 038 $cnt = 0; 039 while ($line = fgets($ThumbnailDataHandle)) { 040 $word = preg_split("/[\t\n]/", $line); 041 $ThumbnailImageArray[$cnt]['path'] = $word[0]; 042 $cnt++; 043 } 044 $ThumbnailImageNum = $cnt; 045 046 fclose($ThumbnailDataHandle); 047 048 if(!$SilentFlag) { 049 printf("<div>found %d thumbnails.</div>\n", $ThumbnailImageNum); 050 } 051 052 // Read from SQL server to set AssetImageArray. 053 if (!$link = mysql_connect($DBHost, $DBUser, $DBPassword)) { 054 die("mysql_connect(ERROR): " . mysql_error()); 055 } 056 if (!mysql_select_db($Database)) { 057 die("mysql_select_db(ERROR): " . mysql_error()); 058 } 059 if (!$result = mysql_query("SELECT asset_file_path FROM mt_asset WHERE asset_class = 'image' AND asset_blog_id = '2'")) { 060 die("mysql_query(ERROR): " . mysql_error()); 061 } 062 063 $cnt = 0; 064 while ($line = mysql_fetch_row($result)) { 065 $AssetImageArray[$cnt]['path'] = $line[0]; 066 $AssetImageArray[$cnt]['match'] = 0; 067 $cnt++; 068 } 069 $AssetImageNum = $cnt; 070 071 mysql_close($link); 072 073 if(!$SilentFlag) { 074 printf("<div>found %d asset images.</div>\n", $AssetImageNum); 075 } 076 077 // Check whether ThumbnailImageArray matches to AssetImageArray. 078 $match = 0; 079 for($cntA = 0; $cntA < $AssetImageNum; $cntA++) { 080 for($cntT = 0; $cntT < $ThumbnailImageNum; $cntT++) { 081 if(!strcmp($AssetImageArray[$cntA]['path'], $ThumbnailImageArray[$cntT]['path'])) { 082 $AssetImageArray[$cntA]['match'] = 1; 083 $match++; 084 break; 085 } 086 } 087 } 088 089 if(!$SilentFlag) { 090 printf("<div>matched %d asset images.</div>\n", $match); 091 } 092 093 // Create new ThumbnailImage if it not found in ThumbnailDataFile. 094 $create = 0; 095 for($cnt = 0; $cnt < $AssetImageNum; $cnt++) { 096 if(!$AssetImageArray[$cnt]['match'] || $ForceFlag) { 097 $AssetImagePath = preg_replace('/^%r\//', $BlogBasePath, $AssetImageArray[$cnt]['path']); 098 099 $size = getimagesize($AssetImagePath); 100 if(!$size[0] || !$size[1]) { 101 die("getimagesize(ERROR): $AssetImagePath"); 102 } 103 $AssetImageArray[$cnt]['width'] = $size[0]; 104 $AssetImageArray[$cnt]['height'] = $size[1]; 105 106 switch($size['mime']) { 107 case "image/jpeg": 108 $AssetImage = imagecreatefromjpeg($AssetImagePath); 109 break; 110 case "image/gif": 111 $AssetImage = imagecreatefromgif($AssetImagePath); 112 break; 113 default: 114 $AssetImage = false; 115 break; 116 } 117 118 $ThumbnailImage = imagecreatetruecolor($ThumbnailWidth, $ThumbnailHeight); 119 120 if(($AssetImageArray[$cnt]['width'] / $AssetImageArray[$cnt]['height']) > ($ThumbnailWidth / $ThumbnailHeight)) { 121 $width = round($AssetImageArray[$cnt]['width'] * $ThumbnailHeight / $AssetImageArray[$cnt]['height']); 122 $height = $ThumbnailHeight; 123 $srcw = round($AssetImageArray[$cnt]['height'] * $ThumbnailWidth / $ThumbnailHeight); 124 $srch = $AssetImageArray[$cnt]['height']; 125 $srcx = round(($AssetImageArray[$cnt]['width'] - $srcw) / 2.0); 126 $srcy = 0; 127 } else { 128 $width = $ThumbnailWidth; 129 $height = round($AssetImageArray[$cnt]['height'] * $ThumbnailWidth / $AssetImageArray[$cnt]['width']); 130 $srcw = $AssetImageArray[$cnt]['width']; 131 $srch = round($AssetImageArray[$cnt]['width'] * $ThumbnailHeight / $ThumbnailWidth); 132 $srcx = 0; 133 $srcy = round(($AssetImageArray[$cnt]['height'] - $srch) / 2.0); 134 } 135 imagecopyresampled($ThumbnailImage, $AssetImage, 0, 0, $srcx, $srcy, $ThumbnailWidth, $ThumbnailHeight, $srcw, $srch); 136 137 $path = pathinfo($AssetImagePath); 138 $ThumbnailImagePath = sprintf("%s/%s_%dx%d.%s", $path['dirname'], $path['filename'], $ThumbnailWidth, $ThumbnailHeight, $path['extension']); 139 140 switch($size['mime']) { 141 case "image/jpeg": 142 imagejpeg($ThumbnailImage, $ThumbnailImagePath, 80); 143 break; 144 case "image/gif": 145 imagegif($ThumbnailImage, $ThumbnailImagePath); 146 break; 147 default: 148 $AssetImage = false; 149 break; 150 } 151 152 imagedestroy($ThumbnailImage); 153 imagedestroy($AssetImage); 154 155 if(!$SilentFlag) { 156 if($create % 2) { 157 $bgcolor = "#f8f8f8"; 158 } else { 159 $bgcolor = "#f0f0f0"; 160 } 161 printf("<div style=\"background-color: $bgcolor;\">create thumbnail from [%s] to [%s].</div>\n", $AssetImagePath, $ThumbnailImagePath); 162 printf("<div style=\"background-color: $bgcolor;\"><img src=\"%s\" width=\"%s\" height=\"%s\" /> <img src=\"%s\" width=\"%s\" height=\"%s\" /> (%s)</div>\n", $AssetImagePath, $width, $height, $ThumbnailImagePath, $ThumbnailWidth, $ThumbnailHeight, $size['mime']); 163 } 164 165 $create++; 166 } 167 } 168 169 if(!$SilentFlag) { 170 printf("<div>created %d thumbnails.</div>\n", $create); 171 } 172 173 // Write back to ThumbnailDataFile from ThumbnailImageArray. 174 if($ForceFlag) { 175 $mode = "w"; 176 } else { 177 $mode = "a"; 178 } 179 if(!$ThumbnailDataHandle = fopen($BlogBasePath . $ThumbnailDataPath, $mode)) { 180 die("fopen(ERROR): $BlogBasePath$ThumbnailDataPath"); 181 } 182 183 for($cnt = 0; $cnt < $AssetImageNum; $cnt++) { 184 if(!$AssetImageArray[$cnt]['match'] || $ForceFlag) { 185 fprintf($ThumbnailDataHandle, "%s\n", $AssetImageArray[$cnt]['path']); 186 } 187 } 188 189 fclose($ThumbnailDataHandle); 190 ?> 191 </body> 192 </html>