php - How to resize/crop or reduce the size of an image displayed from SQL Server query? -
i want minimize size of sql server image before gets loaded on clients.
unfortunately example not implemented: resizing , displaying blob element database
code:
<?php include "dbconf2.php"; $sqlimage=" select f.grafik, f.knr, f.brstatus1, f.untertitel1, f.anzeigeweb1 foto f f.knr 'fhtg20900%' ;"; $stmtimage = sqlsrv_query($connvhs, $sqlimage); while ($result2=sqlsrv_fetch_array($stmtimage)) { echo "titel: ".$result2['untertitel1']."<br>"; echo "knr: ".$result2['knr']."<br>"; echo "status: ".$result2['brstatus1']."<br>"; echo "web: ".$result2['anzeigeweb1']."<br>"; echo '<img src="data:image/jpeg;base64,'.base64_encode( $result2['grafik']).'" style="max-height: 500px; max-width: 400px;"/>'."<br>"; echo "<br>"; } ?>
edit: simple implementation:
$image = imagecreatefromstring(base64_encode( $result2['grafik'])); $image = imagescale($image, 100, 100); ob_start(); imagejpeg($image); $contents = ob_get_contents(); ob_end_clean(); echo "<img src='data:image/jpeg;base64,".base64_encode($contents)."' />"; imagedestroy($image);
but these errors:
warning: imagecreatefromstring(): data not in recognized format ...
warning: imagescale() expects parameter 1 resource, boolean given in ...
warning: imagedestroy() expects parameter 1 resource, null given in ...
this might you,but have first create actual image sql. it's better if can save image file actual file.it far more faster.
function resize_image($filename, $tmpname, $xmax, $ymax) { $ext = explode(".", $filename); $ext = $ext[count($ext)-1]; if($ext == "jpg" || $ext == "jpeg") $im = imagecreatefromjpeg($tmpname); elseif($ext == "png") $im = imagecreatefrompng($tmpname); elseif($ext == "gif") $im = imagecreatefromgif($tmpname); $x = imagesx($im); $y = imagesy($im); if($x <= $xmax && $y <= $ymax) return $im; if($x >= $y) { $newx = $xmax; $newy = $newx * $y / $x; } else { $newy = $ymax; $newx = $x / $y * $newy; } $im2 = imagecreatetruecolor($newx, $newy); imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y); return $im2; }
Comments
Post a Comment