Tuesday, December 10, 2013

PHP image resizer

I've left code in this example specific to the project just to show more lessons than just image resizing. Core lesson is in the code lines with getimagesize and desiredSize variable. Cheers.

<?php
print "<html>\n<head>\n<title>photo server</title>\n</head>\n<body>\n";
if(isset($_REQUEST['workorderNumber'])) { $wo=$_REQUEST['workorderNumber'];}
 else { $wo="000000"; }
$files = glob(getcwd()."/*".$wo."*");
$iter=0;
print "\n<table border=\"true\"><tr>\n";
foreach($files as $file) {
 $url="http://".$_SERVER["SERVER_NAME"]."/uploads/";
 $sizeimg=1; 
 $size = getimagesize(getcwd()."/".basename($file));
 $width = $size[0]; // Index 0
 $height = $size[1]; // Index 1
 $type = $size[2]; // Index 2
 $desiredSize = 250;
 while(($height*$sizeimg) > $desiredSize) { $sizeimg= $sizeimg - .05;  }
 $nwidth=$width*$sizeimg;
 $nheight=$height*$sizeimg;
 echo "<td valign=\"top\">\n";
 print "\n<a href=\"".$url.basename($file)."\" target=\"_blank\">\n";
 print "\n<img src=\"".$url.basename($file)."\" width=\"".$nwidth."\" height=\"".$nheight."\" />\n";
 print "\n</a><br />\n"; 
 $fileArray=explode("_",basename($file));
 print $fileArray[0]." ".$fileArray[1]." ".$fileArray[5]." ".$fileArray[6]."\n</td>\n";     
 $iter++;
 if(($iter%5)==0) { print "\n</tr><tr>\n" ;}
}
?>
</tr></table>
</body>
</html>

No comments: