Php Store将图像调整为变量

时间:2012-12-28 05:47:24

标签: php image

我想将输出图像存储为变量,因此我可以在其上运行循环。我想知道我怎么做到这一点?我很困惑如何用imagejpeg做到这一点?就像我希望代码最终能够用echo $ image显示图像。

 $imagequery = mysql_query("SELECT source FROM photos ORDER BY id DESC LIMIT 16");

for($ iii = 0; $ iii< 16; $ iii ++){

$ imagetrial = mysql_result($ imagequery,$ iii,'source'); $ imageSrc = imagecreatefromstring($ imagetrial);

 $width = "300";

 if (is_numeric($width) && isset($imageSrc)){
 header('Content-type: image/jpeg');
 makeThumb($imageSrc, $width);
  }

function makeThumb($src,$newWidth) {

$srcImage = imagecreatefromjpeg($src);
$width = imagesx($srcImage);
 $height = imagesy($srcImage);

 $newHeight = floor($height*($newWidth/$width));

  $newImage = imagecreatetruecolor($newWidth,$newHeight);

  imagecopyresized($newImage,$srcImage,0,0,0,0,$newWidth,$newHeight,$width,$height);

   imagejpeg($newImage);
 }

}

2 个答案:

答案 0 :(得分:0)

查看函数imagecreatefromstring

http://php.net/manual/en/function.imagecreatefromstring.php

$image = imagecreatefromstring($imageSrc);

if ($image !== FALSE) {
  // the variable is now a valid image resource
}

对于您的查询,它返回多行。您需要逐个获取行。

$result = mysql_query("SELECT source FROM photos ORDER BY id DESC LIMIT 16");

while ($row = mysql_fetch_assoc($result)) {
  $image = imagecreatefromstring($row['source']);

  if ($image !== FALSE) {
    // the variable is now a valid image resource
  }
}
然而,它将极其耗费资源。更好的解决方案是将图像存储在磁盘上,并在数据库中显示图像的路径。

另请注意,不推荐使用过程mysql函数。你应该转移到mysqli。

http://fr2.php.net/manual/en/book.mysqli.php

编辑:您的问题没有说明您想要的内容,无论如何。此代码(未经测试)将以类似画廊的方式并排绘制缩略图。有一些方法可以简化它,但我已经写好了,所以很容易理解。

$num_columns = 4; // the number of thumbnails per row
$thumb_width = 400;
$thumb_height = 300;

$result = mysql_query("SELECT source FROM photos ORDER BY id DESC LIMIT 16");

// the actual number of results
$num_photos = mysql_num_rows($result);
$num_rows = ceil($num_photos / $num_columns);

$gallery_width = $num_columns * $thumb_width;
$gallery_height = $num_rows * $thumb_height;

// create a large empty image that will fit all thumbnails
$gallery = imagecreatetruecolor($gallery_width, $gallery_height);

$x = 0;
$y = 0;

// fetch the images one by one
while ($row = mysql_fetch_assoc($result)) {
  $image = imagecreatefromstring($row['source']);

  // the variable is now a valid image resource
  if ($image !== FALSE) {
    // grab the size of the image
    $image_width = imagesx($image);
    $image_height = imagesy($image);

    // draw and resize the image to the next position in the gallery
    imagecopyresized($gallery, $image, $x, $y, 0, 0, $thumb_width, $thumb_height, $image_width, $image_height);

    // move the next drawing position to the right
    $x += $thumb_width;

    // if it has reached the far-right then move down a row and reset the x position
    if ($x >= $gallery_width) {     
        $y += $thumb_height;
        $x = 0;
    }

    // destroy the resource to free the memory
    imagedestroy($image);
  }
}
mysql_free_result($result);

// send the gallery image to the browser in JPEG
header('Content-Type: image/jpeg');
imagejpeg($gallery);

编辑:错误修复了代码

答案 1 :(得分:0)

要将图像内容存储到变量中,您必须将拇指图像保存到特定路径,然后使用 file_get_content()函数

检索图像内容

您可以通过将目标路径作为参数传递到 imagejpeg 功能

来保存最终的拇指图像

作为您问题的解决方案,请参阅下面提到的示例代码段

           $width = "300";
        $thumb_image_file=$_SERVER['DOCUMENT_ROOT'].'/thumbs/abc.jpg';
        if (is_numeric($width) && isset($imageSrc)){
        header('Content-type: image/jpeg');
        makeThumb($imageSrc, $width);
        $img_content=file_get_contents($thumb_image_file);
       echo $img_content;
   }

     function makeThumb($src,$newWidth,$thumb_image_file) {


               $srcImage = imagecreatefromjpeg($src);
             $width = imagesx($srcImage);
            $height = imagesy($srcImage);

        $newHeight = floor($height*($newWidth/$width));

        $newImage = imagecreatetruecolor($newWidth,$newHeight);

              imagecopyresized($newImage,$srcImage,0,0,0,0,$newWidth,$newHeight,$width,$height);


                 imagejpeg($newImage,$thumb_image_file);
   }