Jump to content

Help with extracting pictures from array to html


enedene

Recommended Posts

I'm using Ubuntu Linux, apache 2.2, php 5.2.6

 

I want to make a auto gallery. You put files in one directory, php script looks in that directory and makes a simple gallery.

I want it to be done without writing to hard drive. I have found a script that creates thumbnails and writes them to some other directory. I modified the script so instead writing on disk it returns array of pictures.

<?php
function createThumbs($pathToImages, $thumbWidth)
{
// open the directory
$dir=opendir($pathToImages);
$i=0;

// loop through it, looking for any/all JPG files:
while(false!==($fname=readdir($dir))) {

	// parse path for the extension (jpg)
	$info=pathinfo($pathToImages.$fname);

	// continue only if this is a JPEG image
	if (strtolower($info['extension'])=='jpg')
	{
		// load image and get image size
		$img=imagecreatefromjpeg("{$pathToImages}{$fname}");
		$width=imagesx($img);
		$height=imagesy($img);

		// calculate thumbnail size
		$new_width=$thumbWidth;
		$new_height=floor($height*($thumbWidth/$width));
		// create a new temporary image
		$tmp_img[$i]=imagecreatetruecolor($new_width, $new_height);

		// copy and resize old image into new image
		imagecopyresized($tmp_img[$i], $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

		$i++;
	}
}
// close the directory
closedir($dir);
return $tmp_img;
}
?>

 

The problem is that I don't know how to put these pictures in html. For example if I try directly, let's say for first two pictures in array:

<html>
<body>
<?php
$picture=createThumbs("pictures/", 140);
imagejpeg($picture[0]);
imagejpeg($picture[1]);
?>
</body>
</html>

The output I get is raw, just a bunch symbols for each picture.

I have read that people used the Header("Content-type: image/jpeg") function, but I'm not sure that I understand how it works. Nevertheless, I tried it, I put it on the beginning of php script. Now the output gives the first picture, but not the second picture, so I don't know how should I get the rest.

Ideally I would like to have an option to put it in html like <img src=from_array> but I don't know how to do that.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.