Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 26, 2006, 11:47 AM   #1
jtalerico
macrumors 6502
 
Join Date: Nov 2005
PHP help

I am trying to get a random picture script working.... I have used PHP before, but not recently...
<?php
$random = (rand()%3)+1;
$handle = fopen("/Users/joetalerico/Sites/images/"&random".jpg", "wb");
?>

So what i have is a directory that has images numberd 1 -3 1.jpg, 2.jpg, 3.jpg.... I want to generate a number 1-3 and then take that string/int and put it into the path name to be opened. If i am doing this all wrong please dont write the code for me, just point me in the right direction. Thanks!
__________________
iPhone4
17" MacBook Pro i7 500GB SSD
MacPro 2011
jtalerico is offline   0 Reply With Quote
Old Jan 26, 2006, 12:38 PM   #2
aristobrat
macrumors Demi-God
 
Join Date: Oct 2005
Location: Virginia Beach
Remember that PHP uses "." to join strings together, not "&".

For example,

$date= "26";
print "Today is January " . $date. ", 2006";
aristobrat is offline   0 Reply With Quote
Old Jan 26, 2006, 12:50 PM   #3
Mitthrawnuruodo
Moderator
 
Mitthrawnuruodo's Avatar
 
Join Date: Mar 2004
Location: Bergen, Norway
Try to start with:
Code:
<?php
$random = (rand()%3)+1;
$imgstr = "/Users/joetalerico/Sites/images/" . $random . ".jpg";
?>
...and why fopen()? Where are you opening this php script?
__________________
Those who fail to learn history are doomed to repeat it; those who fail to learn history correctly... why, they are simply doomed.
Mitthrawnuruodo is offline   0 Reply With Quote
Old Jan 26, 2006, 12:50 PM   #4
jtalerico
Thread Starter
macrumors 6502
 
Join Date: Nov 2005
Thanks, now here is what i got....


<?php
$random = (rand()%3)+1;
echo "<br>";
echo $random;
$filename = "images/".$random.".jpg";
$handle = fopen($filename, "rb");
$contents = fread($handle,filesize($filename));
?>


But the image wont display. What should i look at?

i try echo $contents but it just gives me the ascii of the image.
__________________
iPhone4
17" MacBook Pro i7 500GB SSD
MacPro 2011
jtalerico is offline   0 Reply With Quote
Old Jan 26, 2006, 12:53 PM   #5
Mitthrawnuruodo
Moderator
 
Mitthrawnuruodo's Avatar
 
Join Date: Mar 2004
Location: Bergen, Norway
If you want to display an image at a website it should be something like this:

Code:
echo "<img src='" . $imgstr . "' alt='description' />";
__________________
Those who fail to learn history are doomed to repeat it; those who fail to learn history correctly... why, they are simply doomed.
Mitthrawnuruodo is offline   0 Reply With Quote
Old Jan 26, 2006, 12:58 PM   #6
jtalerico
Thread Starter
macrumors 6502
 
Join Date: Nov 2005
okay, i guess i have to do it that way, i thought there was a different way of doing it.. Guess i was wrong...

Thanks!
__________________
iPhone4
17" MacBook Pro i7 500GB SSD
MacPro 2011
jtalerico is offline   0 Reply With Quote
Old Jan 26, 2006, 01:41 PM   #7
jtalerico
Thread Starter
macrumors 6502
 
Join Date: Nov 2005
New question

say i want to check to see if a file has the extension of .jpg

how would i do it?

if(is_file($file)) {
echo "<bhabha>";
}

Is there like a is_image ? kind of thing that you guys know of? Thanks!
__________________
iPhone4
17" MacBook Pro i7 500GB SSD
MacPro 2011
jtalerico is offline   0 Reply With Quote
Old Jan 26, 2006, 01:46 PM   #8
Mitthrawnuruodo
Moderator
 
Mitthrawnuruodo's Avatar
 
Join Date: Mar 2004
Location: Bergen, Norway
Something like exif_imagetype()?
__________________
Those who fail to learn history are doomed to repeat it; those who fail to learn history correctly... why, they are simply doomed.
Mitthrawnuruodo is offline   0 Reply With Quote
Old Jan 26, 2006, 02:00 PM   #9
jtalerico
Thread Starter
macrumors 6502
 
Join Date: Nov 2005
You are the man!!
__________________
iPhone4
17" MacBook Pro i7 500GB SSD
MacPro 2011
jtalerico is offline   0 Reply With Quote
Old Jan 26, 2006, 09:24 PM   #10
jtalerico
Thread Starter
macrumors 6502
 
Join Date: Nov 2005
echo "<table cellpadding=0 cellspacing=0 border=1 width=500>\n";
echo "<tr>";
echo "<td bgcolor=light grey>\n";
$dir = opendir('images/banner/');
while(false != ($file = readdir($dir))){
if(exif_imagetype("$file")){
echo "<img src=".$file.">";
}
}

Doesnt seem to be working like i thought....


Warning: exif_imagetype() [function.exif-imagetype]: Read error! in /Users/joetalerico/Sites/global.inc on line 19

Warning: exif_imagetype() [function.exif-imagetype]: Read error! in /Users/joetalerico/Sites/global.inc on line 19

Warning: exif_imagetype(2005_0105Image_30012.jpg) [function.exif-imagetype]: failed to open stream: No such file or directory in /Users/joetalerico/Sites/global.inc on line 19
__________________
iPhone4
17" MacBook Pro i7 500GB SSD
MacPro 2011
jtalerico is offline   0 Reply With Quote
Old Jan 26, 2006, 09:34 PM   #11
jtalerico
Thread Starter
macrumors 6502
 
Join Date: Nov 2005
Fixed some parts of the code.... Still getting errors..

$dir = opendir('images/banner/');
while(false != ($file = readdir($dir))){
if(exif_imagetype("images/banner/"$file) == IMAGETYPE_JPEG){
echo "<img src=images/banner/".$file.">";


Warning: exif_imagetype() [function.exif-imagetype]: Read error! in /Users/joetalerico/Sites/global.inc on line 19

Warning: exif_imagetype() [function.exif-imagetype]: Read error! in /Users/joetalerico/Sites/global.inc on line 19
__________________
iPhone4
17" MacBook Pro i7 500GB SSD
MacPro 2011
jtalerico is offline   0 Reply With Quote
Old Jan 26, 2006, 10:51 PM   #12
Poeben
macrumors 6502
 
Join Date: Jul 2004
Not the most elegant way in your case, but you could use the substr() function.

something like:

substr("filename.jpg", -4); // will return ".jpg" as string
substr("filename.jpeg", -5); // will return ".jpeg" as string

then you can dance your $var == ".jpg" ditty

I have done this sort of thing when dealing with many different file types. There is probably a better way, but whatever.
__________________
ćActual post may vary from original thought.
Poeben is offline   0 Reply With Quote
Old Jan 27, 2006, 05:17 AM   #13
jtalerico
Thread Starter
macrumors 6502
 
Join Date: Nov 2005
what does the -4, -5 thing mean? I am trying to open ALL images in a dir.
__________________
iPhone4
17" MacBook Pro i7 500GB SSD
MacPro 2011
jtalerico is offline   0 Reply With Quote
Old Jan 27, 2006, 06:40 AM   #14
jtalerico
Thread Starter
macrumors 6502
 
Join Date: Nov 2005
Mitthrawnuruodo -- any ideas?

I have tried try-catch blocks to catch the file read error.. But still nothing... here is the code that i am working with right now...

$dir = opendir('images/banner/');
while(false != ($file = readdir($dir))){
if(getimagesize("images/banner/$file") == 2){
echo "<img src=images/banner/".$file.">";
}
}

I know it is really ugly but i would like to get it to work. I am still getting the read errors.. I know the file is there. And i have done the echo "images/banner/$file" to make sure it is there, and it worked. But i cannot get if(getimagesize("images/banner/$file") == 2){ that statement to be happy!
__________________
iPhone4
17" MacBook Pro i7 500GB SSD
MacPro 2011
jtalerico is offline   0 Reply With Quote
Old Jan 27, 2006, 08:50 AM   #15
Mitthrawnuruodo
Moderator
 
Mitthrawnuruodo's Avatar
 
Join Date: Mar 2004
Location: Bergen, Norway
I don't really know...

But...
Code:
while(false != ($file = readdir($dir)))
should be
Code:
while(($file = readdir($dir)) != false)
or just
Code:
while($file = readdir($dir))
should also work because the while exits when the expression is false, anyway...

and getimagesize() returns an array, so seeing if it equals 2 might not give many true results...

and
Code:
echo "<img src=images/banner/".$file.">";
will not validate, and should be
Code:
echo "<img src='images/banner/".$file."' alt='Description' />";
or
Code:
echo "<img src=\"images/banner/".$file."\" alt=\"Description\" />";
__________________
Those who fail to learn history are doomed to repeat it; those who fail to learn history correctly... why, they are simply doomed.
Mitthrawnuruodo is offline   0 Reply With Quote
Old Jan 27, 2006, 09:31 AM   #16
jtalerico
Thread Starter
macrumors 6502
 
Join Date: Nov 2005
Cool! I think i am getting somewhere...

http://69.68.181.132/~joetalerico/gallery.php

I am still getting read errors, is there a way to throw them? Because not everything in there is a jpeg.. So maybe a try catch again?

<?
$dir = opendir('images/banner/');
while($file = readdir($dir)){
if(exif_imagetype("images/banner/$file") == IMAGETYPE_JPEG){
echo "<img src=images/banner/".$file.">";
}
}
?>
__________________
iPhone4
17" MacBook Pro i7 500GB SSD
MacPro 2011
jtalerico is offline   0 Reply With Quote
Old Jan 27, 2006, 09:45 AM   #17
Mitthrawnuruodo
Moderator
 
Mitthrawnuruodo's Avatar
 
Join Date: Mar 2004
Location: Bergen, Norway
Quote:
Originally Posted by jtalerico
http://69.68.181.132/~joetalerico/gallery.php

I am still getting read errors, is there a way to throw them? Because not everything in there is a jpeg.. So maybe a try catch again?
Those two warnings you get:
Quote:
Warning: exif_imagetype() [function.exif-imagetype]: Read error! in /Users/joetalerico/Sites/gallery.php on line 4
What excactly are you trying to read/feed the exif_imagetype() function...?

Maybe try to write out what you're doing (<-- Best debugging tip I can give ), something like:
PHP Code:
while($file readdir($dir)){
    echo 
"Next file: images/banner/" $file "<br />"// Debugging
    
if(exif_imagetype("http://cdn.macrumors.com/vb/images/banner/$file") == IMAGETYPE_JPEG){ 
__________________
Those who fail to learn history are doomed to repeat it; those who fail to learn history correctly... why, they are simply doomed.
Mitthrawnuruodo is offline   0 Reply With Quote
Old Jan 27, 2006, 09:51 AM   #18
jtalerico
Thread Starter
macrumors 6502
 
Join Date: Nov 2005
Quote:
Originally Posted by Mitthrawnuruodo
Those two warnings you get:
What excactly are you trying to read/feed the exif_imagetype() function...?

Maybe try to write out what you're doing (<-- Best debugging tip I can give ), something like:
PHP Code:
while($file readdir($dir)){
    echo 
"Next file: images/banner/" $file "<br />"// Debugging
    
if(exif_imagetype("http://cdn.macrumors.com/vb/images/banner/$file") == IMAGETYPE_JPEG){ 

Yup i have done that! The problem is it is not a image, i want to throw that exception.. I guess when it is not a image it will show that error.. Check it out now..
http://69.68.181.132/~joetalerico/gallery.php
__________________
iPhone4
17" MacBook Pro i7 500GB SSD
MacPro 2011
jtalerico is offline   0 Reply With Quote
Old Jan 27, 2006, 10:13 AM   #19
Mitthrawnuruodo
Moderator
 
Mitthrawnuruodo's Avatar
 
Join Date: Mar 2004
Location: Bergen, Norway
Quote:
Originally Posted by jtalerico
Yup i have done that! The problem is it is not a image, i want to throw that exception.. I guess when it is not a image it will show that error.. Check it out now..
http://69.68.181.132/~joetalerico/gallery.php
The problem is the folder structure, you'll have to not call exit_imagetype() for this folder . and the parent folder ..

Other than that it seem to work...
__________________
Those who fail to learn history are doomed to repeat it; those who fail to learn history correctly... why, they are simply doomed.
Mitthrawnuruodo is offline   0 Reply With Quote
Old Jan 27, 2006, 10:26 AM   #20
jtalerico
Thread Starter
macrumors 6502
 
Join Date: Nov 2005
Right.. But how can i get it to skip those two folders, they are the first thing it will hit.

maybe something like if(($dir = .) || ($dir = ..)){

Some how skip it?
__________________
iPhone4
17" MacBook Pro i7 500GB SSD
MacPro 2011
jtalerico is offline   0 Reply With Quote
Old Jan 27, 2006, 11:10 AM   #21
jeremy.king
macrumors 603
 
jeremy.king's Avatar
 
Join Date: Jul 2002
Location: Fox Lake, IL
Quote:
Originally Posted by jtalerico
Right.. But how can i get it to skip those two folders, they are the first thing it will hit.

maybe something like if(($dir = .) || ($dir = ..)){

Some how skip it?
Could try to use the is_dir() function or just check the name

PHP Code:
while($file readdir($dir)){
    echo 
"Next file: images/banner/" $file "<br />"// Debugging
    
if(!is_dir($file) && (exif_imagetype("http://cdn.macrumors.com/vb/images/banner/$file") == IMAGETYPE_JPEG)){ 
or

PHP Code:
while($file readdir($dir)){
    echo 
"Next file: images/banner/" $file "<br />"// Debugging
    
if(!($file == "." || $file == "..") && (exif_imagetype("http://cdn.macrumors.com/vb/images/banner/$file") == IMAGETYPE_JPEG)){ 
P.S. Remember http://www.php.net has a very extensive language reference with examples.
jeremy.king is offline   0 Reply With Quote
Old Jan 27, 2006, 11:54 AM   #22
jtalerico
Thread Starter
macrumors 6502
 
Join Date: Nov 2005
Quote:
Originally Posted by kingjr3
Could try to use the is_dir() function or just check the name

PHP Code:
while($file readdir($dir)){
    echo 
"Next file: images/banner/" $file "<br />"// Debugging
    
if(!is_dir($file) && (exif_imagetype("http://cdn.macrumors.com/vb/images/banner/$file") == IMAGETYPE_JPEG)){ 
or

PHP Code:
while($file readdir($dir)){
    echo 
"Next file: images/banner/" $file "<br />"// Debugging
    
if(!($file == "." || $file == "..") && (exif_imagetype("http://cdn.macrumors.com/vb/images/banner/$file") == IMAGETYPE_JPEG)){ 
P.S. Remember http://www.php.net has a very extensive language reference with examples.

I have been using php.net alot!! thanks, ill give it a try!
__________________
iPhone4
17" MacBook Pro i7 500GB SSD
MacPro 2011
jtalerico is offline   0 Reply With Quote
Old Jan 27, 2006, 02:51 PM   #23
Stampyhead
macrumors 68020
 
Stampyhead's Avatar
 
Join Date: Sep 2004
Location: London, UK
You should check out the Sitepoint forums too (www.sitepoint.com). There are some very knowledgeable PHP gurus over there. They are they ones I pitch my toughest PHP problems to and they have always been very helpful.
Stampyhead is offline   0 Reply With Quote
Old Jan 27, 2006, 07:17 PM   #24
Mitthrawnuruodo
Moderator
 
Mitthrawnuruodo's Avatar
 
Join Date: Mar 2004
Location: Bergen, Norway
Quote:
Originally Posted by Nermal
Quote:
Originally Posted by Stampyhead
You should check out the Sitepoint forums too (www.sitepoint.com). There are some very knowledgeable PHP gurus over there. They are they ones I pitch my toughest PHP problems to and they have always been very helpful.
I'm going to second this. I haven't actually looked at the forums, but I learnt PHP from a Sitepoint book
Hey... what's wrong with the Mac programming forum here at MR...?

Ok, with a little help from php.net...
__________________
Those who fail to learn history are doomed to repeat it; those who fail to learn history correctly... why, they are simply doomed.
Mitthrawnuruodo is offline   0 Reply With Quote
Old Jan 27, 2006, 09:24 PM   #25
Nermal
Moderator
 
Nermal's Avatar
 
Join Date: Dec 2002
Location: Whakatane, New Zealand
Forgery! Muahahaha!
Nermal is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
[php] - php problems os x mountain lion 10.8.2 cakas6933 OS X 10.8 Mountain Lion 4 Jun 10, 2014 08:08 AM
PHP - stream_context_create larswik Mac Programming 13 Jun 28, 2012 10:00 PM
mylove.php? Help! :/ postingq Mac Basics and Help 3 Jun 27, 2012 12:10 PM
How many frameworks available in PHP? ifuturzcom iPhone 0 Jun 4, 2012 02:24 AM

Forum Jump

All times are GMT -5. The time now is 09:38 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps