-1

I want to minimize the size of a SQL Server image before it gets loaded on the clients.

Unfortunately I get the example not implemented: Resizing and then displaying BLOB element from database

Code:

<?php
include "dbconf2.php";

$sqlimage="
SELECT
        F.Grafik,
        F.knr,
        F.brstatus1,
        F.untertitel1,
        F.anzeigeweb1

    FROM FOTO F 
    WHERE F.knr LIKE '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: my 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 I get these errors:

Warning: imagecreatefromstring(): Data is not in a recognized format ...

Warning: imagescale() expects parameter 1 to be resource, boolean given in ...

Warning: imagedestroy() expects parameter 1 to be resource, null given in ...

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ru77y
  • 1
  • 1
  • _“This solution doesn´t help”_ - saying this solution doesn’t help without saying _why/how_ it does not help, does not help either. Please go read [ask]. – CBroe Aug 18 '17 at 08:32
  • Sorry, I´m not a pro and don´t get to work ... :-/ – Ru77y Aug 18 '17 at 08:43
  • Well then you still need to show us what you tried. – CBroe Aug 18 '17 at 08:44

1 Answers1

0

This might help you,but you have to first create an actual image from SQL. It's better if you can save image file as actual file.It is 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;   
}