Mitali
Mitali

Reputation: 3

create thumbnails without saving them

I have this code which creates thumbnails and then prints them on the website without storing them somewhere.

<img src ="imageThumbnail.php" alt="some description"/>

this works fine when i did this and it did show me the output.

imageThumbnail.php

header("Content-type: image/png");

$im     = imagecreatefrompng("image.png");
list($width, $height) = getimagesize($im);
$newimage = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($newimage, $im, 0, 0, 0, 0, "100", "100", $width, $height);

imagepng($newimage);
imagedestroy($newimage);
imagedestroy($im);

This code was taken from here creating thumbnails without saving them

Now what i wanted was that to send some data to this imageThumbnail php file and then make a query to the database and get the correct path for the particular data that has been passed as get but the ouput was not as expected and the image doesn't show up.

Html code

  <img src ="imageThumbnail.php?product_code=PD1001" alt="some description"/>

imageThumbnail.php This is the modified code

header("Content-type: image/jpeg");
$productCode=$_GET['product_code'];
require 'connect.inc.php';
$statement=$mysqli->prepare("select `product_img_name` from `products` where `product_code`=?");
$statement->bind_param("s",$productCode);
$statement->execute();
$result=$statement->get_result();
while($row=$result->fetch_object())
    $pathName=$row->product_img_name;

$im=imagecreatefromjpeg("cart/images/".$pathName);
$width=imagesx($im);
$height=imagesy($im);
$newimage=imagecreatetruecolor(116,116);
imagecopyresampled($newimage, $im, 0, 0, 0, 0,'116', '116', $width, $height);
imagejpeg($newimage);
imagedestroy($newimage);
imagedestroy($im);

What is the problem and how do i achieve this ?

Upvotes: 0

Views: 148

Answers (2)

Dimitrios Desyllas
Dimitrios Desyllas

Reputation: 10166

How about:

require 'connect.inc.php';

header("Content-type: image/jpeg");
$productCode=$_GET['product_code'];

$statement=$mysqli->prepare("select `product_img_name` from `products` where `product_code`=?");
$statement->bind_param("s",$productCode);
$statement->execute();
$result=$statement->get_result();
while($row=$result->fetch_object())
    $pathName=$row->product_img_name;

$im=imagecreatefromjpeg("cart/images/".$pathName);
$width=imagesx($im);
$height=imagesy($im);
$newimage=imagecreatetruecolor(116,116);
imagecopyresampled($newimage, $im, 0, 0, 0, 0,'116', '116', $width, $height);
imagejpeg($newimage);
imagedestroy($newimage);
imagedestroy($im);

Upvotes: 1

Tilak Raj
Tilak Raj

Reputation: 1499

Remove that require file in the code and instead use the whole code.

Something like this

<?php
error_reporting(-1);
header("Content-type: image/jpeg");
$productCode=$_GET['product_code'];
$db_username = "root";
$db_password = "";
$host_name = "localhost";
$db_name = 'cakenbake';
$mysqli = new mysqli($host_name, $db_username, $db_password, $db_name);
if ($mysqli->connect_error) {
    die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}

$statement=$mysqli->prepare("select `product_img_name` from `products` where `product_code`=?");
$statement->bind_param("s",$productCode);
if($statement->execute())
{
    $result=$statement->get_result();
    while($row=$result->fetch_object())
        $pathName=$row->product_img_name;
    $im=imagecreatefromjpeg("cart/images/".$pathName);
    $width=imagesx($im);
    $height=imagesy($im);
    $newimage=imagecreatetruecolor(116,116);
    imagecopyresampled($newimage, $im, 0, 0, 0, 0,'116', '116', $width, $height);
    imagejpeg($newimage);
    imagedestroy($newimage);
    imagedestroy($im);
}



?>

Use this code .This works fine.

Upvotes: 0

Related Questions