flickenmaste Posted February 27, 2013 Share Posted February 27, 2013 I am using a php file that will select a random image from a folder and display it on my webpage. <?php /* ------------------------- CONFIGURATION ----------------------- Set $folder to the full path to the location of your images. For example: $folder = '/user/me/example.com/images/'; If the rotate.php file will be in the same folder as your images then you should leave it set to $folder = '.'; */ $folder = '/home/flicken/public_html/images'; $extList = array(); $extList['gif'] = 'image/gif'; $extList['jpg'] = 'image/jpeg'; $extList['jpeg'] = 'image/jpeg'; $extList['png'] = 'image/png'; // You don't need to edit anything after this point. // --------------------- END CONFIGURATION ----------------------- $img = null; if (substr($folder,-1) != '/') { $folder = $folder.'/'; } if (isset($_GET['img'])) { $imageInfo = pathinfo($_GET['img']); if ( isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) && file_exists( $folder.$imageInfo['basename'] ) ) { $img = $folder.$imageInfo['basename']; } } else { $fileList = array(); $handle = opendir($folder); while ( false !== ( $file = readdir($handle) ) ) { $file_info = pathinfo($file); if ( isset( $extList[ strtolower( $file_info['extension'] ) ] ) ) { $fileList[] = $file; } } closedir($handle); if (count($fileList) > 0) { $imageNumber = time() % count($fileList); $img = $folder.$fileList[$imageNumber]; } } if ($img!=null) { $imageInfo = pathinfo($img); $contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ]; header ($contentType); readfile($img); } else { if ( function_exists('imagecreate') ) { header ("Content-type: image/png"); $im = @imagecreate (100, 100) or die ("Cannot initialize new GD image stream"); $background_color = imagecolorallocate ($im, 255, 255, 255); $text_color = imagecolorallocate ($im, 0,0,0); imagestring ($im, 2, 5, 5, "IMAGE ERROR", $text_color); imagepng ($im); imagedestroy($im); } } ?> The code works, but my problem is that you cannot save the image file when you right click and save as. Trying to save the "image" actually save the php file. In my html file, I link to the php with : <img src="http://www.examplewebsite.com/images/rotate.php"> Is there anyway to edit the code to allow you to save the actual image, or at least just a way to display the image path so you can click on that to view the actual image? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/275030-get-the-image-url-from-a-php-file/ Share on other sites More sharing options...
AyKay47 Posted February 27, 2013 Share Posted February 27, 2013 Instead of reading the file to the browser like you are now, embed the image in the html instead. <img src='/path/to/image.jpg' /> That way a user will be able to right click and save the image. Quote Link to comment https://forums.phpfreaks.com/topic/275030-get-the-image-url-from-a-php-file/#findComment-1415480 Share on other sites More sharing options...
flickenmaste Posted February 27, 2013 Author Share Posted February 27, 2013 The point of the php file though is to display a random image each time the page is loaded from my image folder. I know I can just embed the image, but then it will only display that one image. I want to be able to display the random image, and be able to save it. Quote Link to comment https://forums.phpfreaks.com/topic/275030-get-the-image-url-from-a-php-file/#findComment-1415490 Share on other sites More sharing options...
AyKay47 Posted February 27, 2013 Share Posted February 27, 2013 I fail to see the difference between embedding a dynamic random image and reading a random dynamic image to implement your logic. Quote Link to comment https://forums.phpfreaks.com/topic/275030-get-the-image-url-from-a-php-file/#findComment-1415493 Share on other sites More sharing options...
requinix Posted February 28, 2013 Share Posted February 28, 2013 I know I can just embed the image, but then it will only display that one image.He's saying that the "one image" you embed is picked randomly. Quote Link to comment https://forums.phpfreaks.com/topic/275030-get-the-image-url-from-a-php-file/#findComment-1415497 Share on other sites More sharing options...
Solution flickenmaste Posted February 28, 2013 Author Solution Share Posted February 28, 2013 I got it working now. I just turned my whole index.htm file into a php file. I did some code that would just output the image and be able to save it. Quote Link to comment https://forums.phpfreaks.com/topic/275030-get-the-image-url-from-a-php-file/#findComment-1415523 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.