Page 1 of 2
Automatic Image Gallery
Posted: Mon Sep 12, 2011 4:19 am
by Romulas
Hi
I have been trying to make the automatic image gallery work for some time now and i am unable to find what is wrong
I hope someone can help, here is my code:
<?php
if (isset($_GET['img'])) {
if(file_exists($_GET['img'])){
ignore_user_abort(true);
set_time_limit(120);
ini_set('memory_limit', '512M');
$src_size = getimagesize($_GET['img']);
if($src_size ===false){
die("This is not an image");
}
$thumb_width = 200;
$thumb_heigth = 200;
//print_r($src_size);
if ($src_size['mime'] === 'image/jpeg') {
$src=imagecreatefromjpeg($GET['img']);
}else if ($src_size['mime']=== 'image/png'){
$src=imagecreatefrompng($GET['img']);
}else if ($src_size['mime']=== 'image/gif'){
$src=imagecreatefromgif($GET['img']);
}
$src_aspect = round(($src_size[0] / $src_size[1]), 1);
$thumb_aspect = round(($thumb_width /$thumb_height), 1);
if($src_aspect < $thumb_aspect) {
$new_size = array($thumb_width,($thumb_width / $src_size[0])* $src_size[1]);
$src_pos =array(0,($new_size[1] - $thumb_height) /2);
}else if($src_aspect > $thumb_aspect){
$newsize= array(($thumb_width / $src_size[1])* $src_size[0], $thumb_height);
$src_pos =array(($new_size[0] - $thumb_width) /2, 0);
}else{
$new_size =array($thumb_width, $thumb_height);
$src_pos= array(0, 0);
}
if ($new_size[0] < 1) $new_size[0] = 1;
if ($new_size[1] < 1) $new_size[1] = 1;
$thumb =imagecreatetruecolor($thumb_width, $thumb_height);
imagecopyresampled($thumb, $src, 0,0, $src_pos[0],$src_pos[1], $new_size[0],$new_size[1], $src_size[0], $src_size[1]);
if ($src_size['mime'] === 'image/jpeg') {
imagejpeg($thumb, "thumbs/{$_GET['img']}");
}else if ($src_size['mime'] === 'image/png'){
imagepng($thumb, "thumbs/{$_GET['img']}");
}else if ($src_size['mime'] === 'image/gif'){
imagegif($thumb, "thumbs/{$_GET['img']}");
}
header("Location: thumbs/{$_GET['img']}");
}
die();
}
if(is_dir('./thumbs')===false){
mkdir('./thumbs', 0744);
}
$images = glob('*.{jpg,jpeg,gif,png}',GLOB_BRACE);
?>
<html>
<head>
<meta http-equiv="Content Type" content="text/html; charset=utf=8" />
<style type="text/css">
a,img{float:left;}
</style>
<title>Image Gallery</title>
</head>
<body>
<div>
<?php
foreach($images as $image) {
if(file_exists("./thumbs/{$image}")) {
echo "<a href=\"{$image}\"><img scr=\"thumbs/{$image}\" alt=\"{$image}\" /></a>";
}else{
echo "<a href=\"{$image}\"><img scr=\"?img={$image}\" alt=\"{$image}\" /></a>";
}
}
?>
</div>
</body>
</html>
All i get when i run this code is the blue rectangles with the file name written in them, it seems the the thumbnail part
does'nt work. The code does create the the thumbs folder.
At this point(code below) the Betterphp video outputs the blue rectangles with the file name written in them, mine outputs nothing.
However i don't see anything wrong with this part of the code
if(is_dir('./thumbs')===false){
mkdir('./thumbs', 0744);
}
$images = glob('*.{jpg,jpeg,gif,png}',GLOB_BRACE);
?>
<html>
<head>
<meta http-equiv="Content Type" content="text/html; charset=utf=8" />
<style type="text/css">
a,img{float:left;}
</style>
<title>Image Gallery</title>
</head>
<body>
<div>
<?php
foreach($images as $image) {
if(file_exists("./thumbs/{$image}")) {
echo "<a href=\"{$image}\"><img scr=\"thumbs/{$image}\" alt=\"{$image}\" /></a>";
}else{
echo "<a href=\"{$image}\"><img scr=\"?img={$image}\" alt=\"{$image}\" /></a>";
}
}
?>
</div>
</body>
Thank you
Re: Automatic Image Gallery
Posted: Mon Sep 12, 2011 12:17 pm
by jacek
If you rightclick on the image and go to view image you might see the cause of the problem. You are probably getting a php error that is causing the image to be invalid.
Re: Automatic Image Gallery
Posted: Tue Sep 13, 2011 1:46 am
by Romulas
Hi jacek
I don't get your answer, there are no images to right clik on, except for those in the folder that i started with. Anyway i decided to start again and i got to this line
$images=glob(`*.{jpg,jpeg,gif,png}`,GLOB_BRACE);
When i run the program at this point using the print_r i get
Array([0]=>)
Thats not what was displayed on the video, However i can't see anything wrong with the code so far
Thank you
Re: Automatic Image Gallery
Posted: Tue Sep 13, 2011 12:18 pm
by jacek
I meant the gap where the images should be..
Those ` should be '
Thins surrounded by ` will be executed as a shell command.
Re: Automatic Image Gallery
Posted: Wed Sep 14, 2011 2:59 am
by Romulas
Sorry, but i still have no idea of what you want me to do,perhaps a pic would help
Thanks
Re: Automatic Image Gallery
Posted: Wed Sep 14, 2011 7:02 am
by Temor
Romulas wrote:Sorry, but i still have no idea of what you want me to do,perhaps a pic would help
Thanks
You're supposed to change the backticks (
` ) to single quotes (
' )
Re: Automatic Image Gallery
Posted: Fri Sep 16, 2011 12:50 am
by Romulas
Thanks
I removed all the back ticks but it did'nt fix the problem, the script sill does not work.
I think the snippet of code below has something to do with the problem, because when the author of the video executes the
print_r($src_size); it returns this:
Array([0]=> 320[1]=>480[2]=>3[3]=>width="320"height="480"[bit=>48[mime]=>image/png)
When i execute the same snippet i get nothing. Am i on the right track and what do i need to do to fix it?
if (isset($_GET['img'])) {
if(file_exists($_GET['img'])){
ignore_user_abort(true);
set_time_limit(120);
ini_set('memory_limit', '512M');
$src_size = getimagesize($_GET['img']);
if($src_size ===false){
die("This is not an image");
}
$thumb_width = 200;
$thumb_heigth = 200;
print_r($src_size);
Re: Automatic Image Gallery
Posted: Fri Sep 16, 2011 8:17 pm
by jacek
What are you passing in as $_GET['img'] ? Can you post the full code ?
Re: Automatic Image Gallery
Posted: Sat Sep 17, 2011 1:19 am
by Romulas
I posted all the code in my first post at the top of the page
What was passed in to $_GET['img'] in the video? . I did'nt
see that bit
Thanks
Re: Automatic Image Gallery
Posted: Sat Sep 17, 2011 11:22 am
by jacek
Romulas wrote:I posted all the code in my first post at the top of the page
I know, but you have made some changes now.
Romulas wrote:What was passed in to $_GET['img'] in the video? . I did'nt
You may have made a mistake.
Can you post the full code ?
Re: Automatic Image Gallery
Posted: Sun Sep 18, 2011 1:03 am
by Romulas
Hi
Here is all the code
<?php
if (isset($_GET['img'])) {
if(file_exists($_GET['img'])){
ignore_user_abort(true);
set_time_limit(120);
ini_set('memory_limit', '512M');
$src_size = getimagesize($_GET['img']);
if($src_size === false){
die("This is not an image");
}
$thumb_width = 100;
$thumb_heigth = 100;
//print_r($src_size);
if ($src_size['mime'] === 'image/jpeg') {
$src=imagecreatefromjpeg($GET['img']);
}else if ($src_size['mime']=== 'image/png'){
$src=imagecreatefrompng($GET['img']);
}else if ($src_size['mime']=== 'image/gif'){
$src=imagecreatefromgif($GET['img']);
}
$src_aspect = round(($src_size[0] / $src_size[1]), 1);
$thumb_aspect = round(($thumb_width /$thumb_height), 1);
if($src_aspect < $thumb_aspect) {
$new_size = array($thumb_width,($thumb_width / $src_size[0])* $src_size[1]);
$src_pos =array(0,($new_size[1] - $thumb_height) /2);
}else if($src_aspect > $thumb_aspect){
$newsize= array(($thumb_width / $src_size[1])* $src_size[0], $thumb_height);
$src_pos =array(($new_size[0] - $thumb_width) /2, 0);
}else{
$new_size =array($thumb_width, $thumb_height);
$src_pos= array(0, 0);
}
if ($new_size[0] < 1) $new_size[0] = 1;
if ($new_size[1] < 1) $new_size[1] = 1;
$thumb =imagecreatetruecolor($thumb_width, $thumb_height);
imagecopyresampled($thumb, $src, 0,0, $src_pos[0],$src_pos[1], $new_size[0],$new_size[1], $src_size[0], $src_size[1]);
if ($src_size['mime'] === 'image/jpeg') {
imagejpeg($thumb, "thumbs/{$_GET['img']}");
}else if ($src_size['mime'] === 'image/png'){
imagepng($thumb, "thumbs/{$_GET['img']}");
}else if ($src_size['mime'] === 'image/gif'){
imagegif($thumb, "thumbs/{$_GET['img']}");
}
header("Location: thumbs/{$_GET['img']}");
}
die();
}
if(is_dir('./thumbs')===false){
mkdir('./thumbs', 0744);
}
$images = glob('*.{jpg,jpeg,gif,png}',GLOB_BRACE);
?>
<html>
<head>
<meta http-equiv="Content Type" content="text/html; charset=utf=8" />
<style type="text/css">
a,img{float:left;}
</style>
<title>Image Gallery</title>
</head>
<body>
<div>
<?php
foreach($images as $image) {
if(file_exists("./thumbs/{$image}")) {
echo "<a href=\"{$image}\"><img scr=\"thumbs/{$image}\" alt=\"{$image}\" /></a>";
}else{
echo "<a href=\"{$image}\"><img scr=\"?img={$image}\" alt=\"{$image}\" /></a>";
}
}
?>
</div>
</body>
</html>
I watched the videos again and relised how you are passing to $_GET['img'], that was very subtle, at the time I thought you
where demonstarting something, when i tried that in firefox it did'nt work, firefox doesn't have those options in the
right click menu, so i added it into the address bar. the extra bit "?img=picture.jpg" (without the quotes) but it didn't work.
Is it possible that this only works in Chrome. I could be wrong i have been before.
Thank you jacek for your patience, i realy appreciate your help,
Re: Automatic Image Gallery
Posted: Sun Sep 18, 2011 11:12 am
by jacek
Romulas wrote:Is it possible that this only works in Chrome.
Not really since the image processing is done on the server and has nothing to do with the browser.
Romulas wrote: so i added it into the address bar. the extra bit "?img=picture.jpg" (without the quotes) but it didn't work.
What did you see when you did that ? In what way did it not work ?
Re: Automatic Image Gallery
Posted: Mon Sep 19, 2011 3:09 am
by Romulas
This is the address when i run the script, with all the broken image links visible
http://localhost/ImageGallery/gallery.php ( i addedd this)?img=picture.jpg
and i got this
Not Found
The requested URL /ImageGallery/thumbs/picture.jpg was not found on the server
Why is it looking in the thumbs directory for a thumb that has not been created?
Thats all i can tell you,I hope it helps
Thank you
Re: Automatic Image Gallery
Posted: Mon Sep 19, 2011 6:15 pm
by jacek
On line 50 of the last code you posted, the browser is redirected to the file after it has been created.
Try commenting out the header line on line 50 and see if you see the cause of the file not being created then.
Re: Automatic Image Gallery
Posted: Tue Sep 20, 2011 1:38 am
by Romulas
Finally, i am getting somewhere commenting out that line has produced a stack of errors,
I have:
Undefined variable thumb_height line 25,31and 40
Divide by zero line 25
Undefined variable new_size line 31
Undefined offset 1 line 37 and 38
Warning imagecreatetruecolor() invalid image dimention line 40
Warning imagecpoyresampled() expects parameter 1 to be resourse line 41
Warning imagejpeg() expects parameter 1 to be resourse line 44
Any tips for fixing all these errors woul be appreciated
Thak you jacek
Re: Automatic Image Gallery
Posted: Tue Sep 20, 2011 10:22 pm
by jacek
Ah now we are getting somewhere !
Most of those scary errors (divide by zero) will be caused by the undefined variables.
$thumb_heigth = 100;
heigth should be height.
$newsize= array(($thumb_width / $src_size[1])* $src_size[0], $thumb_height);
newsize should be new size
Re: Automatic Image Gallery
Posted: Wed Sep 21, 2011 12:52 am
by Romulas
I have fixed all the errors and it produced a thumbnail(fantastc). The only problem now is
that it only produces the thumbnail i select by adding ?img=picture.jpg to the
addressbar. How can i make it produce all images automatically?
Re: Automatic Image Gallery
Posted: Wed Sep 21, 2011 3:41 pm
by jacek
Romulas wrote:I have fixed all the errors and it produced a thumbnail(fantastc). The only problem now is
that it only produces the thumbnail i select by adding ?img=picture.jpg to the
addressbar. How can i make it produce all images automatically?
It should do that. It will only recreate the images if the thumbnail files don't exist already so you may need to remove the broken ones.
Re: Automatic Image Gallery
Posted: Thu Sep 22, 2011 1:33 am
by Romulas
I deleted the thumbnail and started again, by excuting the the script, should'nt that create thumbs for all the pictures in my case the ImageGallery folder. When you say "remove the broken ones", what do you mean?
Thanks
Re: Automatic Image Gallery
Posted: Thu Sep 22, 2011 11:54 pm
by jacek
If the script fails for some reason it will sometimes generate blank images in the thumb folder, having those there stops it generating new ones.
But if you deleted the that can't be it, can you post your code as it is now ?
Re: Automatic Image Gallery
Posted: Fri Sep 23, 2011 1:38 am
by Romulas
Here is the script
<?php
if (isset($_GET['img'])) {
if(file_exists($_GET['img'])){
ignore_user_abort(true);
set_time_limit(120);
ini_set('memory_limit', '512M');
$src_size = getimagesize($_GET['img']);
if($src_size === false){
die("This is not an image");
}
$thumb_width = 220;
$thumb_height = 240;
if ($src_size['mime'] === 'image/jpeg') {
$src=imagecreatefromjpeg($_GET['img']);
}else if ($src_size['mime']=== 'image/png'){
$src=imagecreatefrompng($_GET['img']);
}else if ($src_size['mime']=== 'image/gif'){
$src=imagecreatefromgif($_GET['img']);
}
$src_aspect = round(($src_size[0] / $src_size[1]), 1);
$thumb_aspect = round(($thumb_width / $thumb_height), 1);
if($src_aspect < $thumb_aspect) {
$new_size = array($thumb_width,($thumb_width / $src_size[0])* $src_size[1]);
$src_pos =array(0,($new_size[1] - $thumb_height) /2);
}else if($src_aspect > $thumb_aspect){
$new_size= array(($thumb_width / $src_size[1])* $src_size[0], $thumb_height);
$src_pos =array(($new_size[0] - $thumb_width) /2, 0);
}else{
$new_size =array($thumb_width, $thumb_height);
$src_pos= array(0, 0);
}
if ($new_size[0] < 1) $new_size[0] = 1;
if ($new_size[1] < 1) $new_size[1] = 1;
$thumb =imagecreatetruecolor($thumb_width, $thumb_height);
imagecopyresampled($thumb, $src, 0,0, $src_pos[0],$src_pos[1], $new_size[0],$new_size[1], $src_size[0], $src_size[1]);
if ($src_size['mime'] === 'image/jpeg') {
imagejpeg($thumb, "thumbs/{$_GET['img']}");
}else if ($src_size['mime'] === 'image/png'){
imagepng($thumb, "thumbs/{$_GET['img']}");
}else if ($src_size['mime'] === 'image/gif'){
imagegif($thumb, "thumbs/{$_GET['img']}");
}
header("Location: thumbs/{$_GET['img']}");
}
die();
}
if(is_dir('./thumbs')===false){
mkdir('./thumbs', 0744);
}
$images = glob('*.{jpg,jpeg,gif,png}',GLOB_BRACE);
?>
<html>
<head>
<meta http-equiv="Content Type" content="text/html; charset=utf=8" />
<style type="text/css">
a,img{float:left;}
</style>
<title>Image Gallery</title>
</head>
<body>
<div>
<?php
foreach($images as $image) {
if(file_exists("./thumbs/{$image}")) {
echo "<a href=\"{$image}\"><img scr=\"thumbs/{$image}\" alt=\"{$image}\" /></a>";
}else{
echo "<a href=\"{$image}\"><img scr=\"?img={$image}\" alt=\"{$image}\" /></a>";
}
}
?>
</div>
</body>
</html>