I have this code below... I would like to resize the image after uploading from mysql... is it possible... anyone can help me on this ?

<?php
 include "file_constants.php";
 // just so we know it is broken
 error_reporting(E_ALL);
 // some basic sanity checks
 if(isset($_GET['id']) && is_numeric($_GET['id'])) {
     //connect to the db
     $link = mysql_connect("$host", "$user", "$pass")
     or die("Could not connect: " . mysql_error());

     // select our database
     mysql_select_db("$db") or die(mysql_error());

     // get the image from the db
     $sql = "SELECT image FROM archive WHERE id=" .$_GET['id'] . ";";

     // the result of the query
     $result = mysql_query("$sql") or die("Invalid query: " . mysql_error());

     // set the header for the image
     header("Content-type: image/jpeg");
     echo mysql_result($result, 0);

     // close the db link
     mysql_close($link);
 }
 else {
     echo 'Please use a real id number';
 }
?>

Thanks in advanced

Dani AI

Generated

As 's snippet shows, that script simply echoes the BLOB after sending Content-type: image/jpeg. That will serve the raw image, but it does not detect image type, validate input, or actually resize. 's suggestion to search and 's linked class are good starting points; below is a compact, practical pattern you can drop into your app. It uses PDO for a safe DB fetch, getimagesizefromstring/imagecreatefromstring to detect format, preserves transparency for PNG/GIF, and resamples with GD.

<?php
// fetch image BLOB securely (PDO) and resize with GD
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($id <= 0) { header('HTTP/1.1 400 Bad Request'); exit; }

$pdo = new PDO($dsn, $dbUser, $dbPass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$stmt = $pdo->prepare('SELECT image FROM archive WHERE id = ?');
$stmt->execute([$id]);
$blob = $stmt->fetchColumn();
if (!$blob) { header('HTTP/1.1 404 Not Found'); exit; }

$info = getimagesizefromstring($blob);
if ($info === false) { header('HTTP/1.1 415 Unsupported Media Type'); exit; }

$src = imagecreatefromstring($blob);
$w = imagesx($src); $h = imagesy($src);
$maxW = 800; $maxH = 600;
$scale = min($maxW / $w, $maxH / $h, 1);
$nw = max(1, (int)($w * $scale));
$nh = max(1, (int)($h * $scale));

$dst = imagecreatetruecolor($nw, $nh);
if ($info['mime'] === 'image/png' || $info['mime'] === 'image/gif') {
    imagealphablending($dst, false);
    imagesavealpha($dst, true);
    $transparent = imagecolorallocatealpha($dst, 0, 0, 0, 127);
    imagefilledrectangle($dst, 0, 0, $nw, $nh, $transparent);
}

imagecopyresampled($dst, $src, 0,0,0,0, $nw, $nh, $w, $h);
header('Content-Type: ' . $info['mime']);
if ($info['mime'] === 'image/png') imagepng($dst);
elseif ($info['mime'] === 'image/gif') imagegif($dst);
else imagejpeg($dst, null, 85);

imagedestroy($src);
imagedestroy($dst);

Troubleshooting and tips: make sure the GD extension is enabled; avoid any output (including BOM or whitespace) before header() calls; check memory_limit for very large originals; free resources with imagedestroy. For production, avoid resizing on every request: generate and cache size variants on disk (for example cache/{id}_{w}x{h}.jpg) and serve the cached file when present. For higher quality and better performance on large files, consider Imagick. Finally, migrate away from mysql_* (removed in PHP 7+) to PDO or mysqli and always validate/sanitize the id before using it in queries.

Recommended Answers

All 2 Replies

There are some PHP image resize libraries. Google it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.