Reputation: 189
I'm attempting to pull files from a directory, compress them, and then display them on my web page. The files are direct uploads from my Digital Camera (when I'm travelling via my iPad) so I cannot shrink them on my computer.
Anyhow, I cannot for the life of me get this to work after many hours of scouring the net.
<?php
echo "<BR><BR>";
$dir = opendir('images/day1/');
while ($read = readdir($dir))
{
if ($read!='.' && $read!='..')
{
echo $read;
exec( 'convert $read -quality 50 $output' );
echo "<BR>";
echo '<a href="images/day1/'.$read.'" TARGET="_BLANK"><img src="'.$output.'" WIDTH="800"></a>';
echo '<BR><BR>';
}
}
closedir($dir);
?>
Any and all suggestions welcomed .... I simply cannot get the convert to work (assign a lesser quality then display the $output.
Thanks in advance
Upvotes: 0
Views: 823
Reputation: 5299
<?php
$photo="sunflower.jpg";
$cmd = "convert $photo -quality 50 JPG:-";
header("Content-type: image/jpeg");
passthru($cmd, $retval);
?>
On a page of its own and can only view the one image or you can have a
<img src="php_page_containing_above_code.php?photo=sunflower.jpg">
and have lots more images. Comment out the $photo variable at the top of the code and I can not remember the exact code at the moment and am at work so can not test it. I do not think you need $photo = $_GET['photo']; but again can not remember as I do not use this method.
Code example added when the OP said the first piece did not work.
Save this as image.php
<?php
$photo = $_REQUEST['photo'];
$cmd = "convert $photo -quality 50 JPG:-";
header("Content-type: image/jpeg");
passthru($cmd, $retval);
?>
Save this as whatever you want and run:
<?php
// Directory to read images from
$dir = "background/";
// Read the directory and sellect EVERYTHING
$filenames = glob( "$dir*" );
// Start the loop to display the images
foreach ( $filenames as $filename ) {
// Display the images
echo "<img src=\"image.php?photo=".$filename."\" />\n";
}
?>
Alternative code:
File 1 - resize images ( DO NOT RUN ON THE DIRECTORY MORE THAN ONCE OR ELSE YOU WILL DOUBLE THE AMOUNT OF THUMBS! )
// Read the directory and sellect jpg only
$filenames = glob("$dir*.{jpg,JPG}", GLOB_BRACE);
// Start the loop to resize the images
foreach ( $filenames as $filename ) {
// New name
$name = explode ( '/', $filename );
$newname = $name[0]."/th_".$name[1];
//Resize and save in the same directory
//exec("convert $filename -resize 400x400 -quality 50 $newname");
}
?>
File 2 - Display images
// Read all the image files into an array that start with th_
$filenames = glob("$dir/th_*.{jpg,JPG}", GLOB_BRACE);
// Display the array contents
foreach ( $filenames as $value ){ echo "<img src=\"$value\"><br>"; }
?>
Upvotes: 1
Reputation: 3251
You use single quotes in this line:
exec( 'convert $read -quality 50 $output' );
This can not work, because the variables $read
and $output
won't be expanded in single quoted strings.
You could use either of the following solutions:
exec( "convert $read -quality 50 $output" ); // double quote string, expands variables
exec( 'convert ' . $read . ' -quality 50 ' . $output ); // single quoted string with concatenated variables
You use $output
in the exec()
line, but you never fill that variable. It should contain the filename where you want convert
to put your converted file.
For debugging, you could also output the command you feed into exec()
:
$cmd = "convert $read -quality 50 $output";
echo $cmd;
exec($cmd);
Upvotes: 0