创建缩略图而不保存它们

时间:2016-03-22 14:54:38

标签: php

我有这个代码可以创建缩略图,然后在网站上打印它们而不将它们存储在某个地方。

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);

imageThumbnail

此代码来自creating thumbnails without saving them

现在我想要的是将一些数据发送到这个Html code php文件,然后对数据库进行查询,并获得已经作为get传递但输出不是特定数据的正确路径如预期的那样,图像没有出现。

<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);

LIKE

问题是什么?我如何实现这个目标?

2 个答案:

答案 0 :(得分:1)

怎么样:

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);

答案 1 :(得分:0)

删除代码中的require文件,而不是使用整个代码。

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);
}



?>

使用此代码。这很好。