0

我想将输出图像存储为变量,这样我就可以在它上面运行循环。我想知道我怎么能做到这一点?我很困惑如何用 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);
 }

}

4

2 回答 2

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);
   }
于 2012-12-28T06:37:47.903 回答
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);

编辑:代码中的错字已修复

于 2012-12-28T05:49:45.793 回答