Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do i show a random image in a php script called from another php script

Posted on 2007-11-15
2
Medium Priority
?
219 Views
Last Modified: 2013-12-13
Hello.  Okay, I'm setting up a small program to display images at random and i want to give people some simple image code to display in their site that uses something like <img src="http://www.mysite.com/myscript.php">  //this calls the server script (myscript.php) that should actually echo out the appropriate image.

 i will only be storing the URL'S to the images in a database and not the raw image data itself.  so basically the server script will produce something like http://www.somesite.com/images/image1.jpg    ..or gif of course...  i intend the server script to randomly choose a url based on some other criteria but what code do i need to use in the server script "myscript.php" to properly display the image in the clients page??

Thanks!
0
Comment
Question by:linuxrox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 21

Accepted Solution

by:
nizsmo earned 2000 total points
ID: 20293121
This should work, you have to change the url to your myscript.php which generates an image.
<?php
function scaleAxis($a, $b){
      //calculate the image axis while maintaining proportion
      return $b / (($a + 1) / $a);
}
 
function scaleImage($width, $height, $maxBoundW, $maxBoundH){
      //tests height vs width and scales the image based on the larger axis
      if ($height > $width){
 
            while ($width + 1 > $maxBoundW) {
                $height -= 1;        
                $width = scaleAxis($height, $width);
            }
           
            while ($height + 1 > $maxBoundH) {
                $height -= 1;        
                $width = scaleAxis($height, $width);
            }
           
      }elseif($width > $height){
             
                  while ($width + 1 > $maxBoundW) {
                      $width -= 1;        
                      $height = scaleAxis($width, $height);
                  }
                 
                  while ($height + 1 > $maxBoundH) {
                      $width -= 1;        
                      $height = scaleAxis($width, $height);
                  }
                 
      }else{
                  while ($width + 1 > $maxBoundW) {
                      $height -= 1;        
                      $width = scaleAxis($height, $width);
                  }
                 
                  while ($height + 1 > $maxBoundH) {
                      $height -= 1;        
                      $width = scaleAxis($height, $width);
                  }
      }
     
      $imageXy['x'] = $width;
      $imageXy['y'] = $height;
     
      return $imageXy;
}
 
// Grab the file from the database or from the directory, then set it as variables
// ; $filetype, $result (which is the file itself)
 
$path = "http://mfr.nizsmo.com/images/logo.jpg"; // YOUR ECHO IMAGE PHP URL HERE!!!
$dimensionX = 96; // adjust dimension as you see fit
$dimensionY = 64;
 
$result = file_get_contents($path);
 
 
      //set the output type
      header("Content-type: Image/Jpeg");
     
      // resize image
      $im = imagecreatefromstring($result);
     
     
      $imageXy = "";
     
      $imageXy = scaleImage(imagesx($im), imagesy($im), $dimensionX,  $dimensionY);  // SPECIFY THUMBNAIL SIZE
     
     
      $imgw = $imageXy['x'];
      $imgh = $imageXy['y'];
     
      // create thumbnail
      $thumb=ImageCreateTrueColor($imgw,$imgh);
      ImageCopyResampled($thumb,$im,0,0,0,0,$imgw,$imgh,ImageSX($im),ImageSY($im));
     
      //display output
      $out = ImagejpeG($thumb);
      print($out);
     
      // clean memory
      imagedestroy ($im);
      imagedestroy ($thumb);
 
 
 
$db->sql_close();
unset($db);
 
 
?>

Open in new window

0
 

Author Closing Comment

by:linuxrox
ID: 31409458
thanks!!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question