Jump to content

Recommended Posts

I have the following script running to show misc. images from a folder on my main page.

 

<?php
function random_image()
{
        /*
        ** Directory
        */
        $dir = "thumbs";
        if(!is_dir($dir))
            die("Directory does not exists");

        /*
        ** Process the directory and read all files in it
        */
        $dirHandle = opendir($dir);
        while(($im = readdir($dirHandle)))
        {
                /*
                ** Do no include sub directories as files
                */
                if($im != ".." && $im != ".")
                {
                        /*
                        ** Create an array for all the files in the directory
                        */
                        $image[] = $im;
                }
        }

        if(!is_array($image))
            die("Unable to read files in directory");

        /*
        ** Close working directory
        */
        closedir($dirHandle);

        /*
        ** Make cache header expire, so its always fresh
        */
        header("Cache-Control: no-cache, must-revalidate");
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

        $n = rand(0,(count($image)-1));
        $random_image = $image[$n];
        /*
        ** Get file extension
        */
        $t_ext = explode(".", $random_image);
        $ext = $t_ext[count($t_ext)-1];
        switch(strtolower($ext))
        {
                case 'jpeg':
                case 'jpg':
                      header("Content-type: image/jpeg");
                break;
                case 'png':
                      header("Content-type: image/png");
                break;
                case 'gif':
                      header("Content-type: image/gif");
                break;
                default:
                /*
                ** Loop if its not an image
                */
                     random_image();
                break;
        }

        $images_ext = array("jpg", "jpeg", "png", "gif");

        if(!file_exists($dir."/".$random_image))
            die("File does not exists");

        /*
        ** Only display if they are images
        */
        if(!readfile($dir."/".$random_image) && !in_array($ext, $images_ext))
        {
                readfile($dir."error/error.gif");
        }
}
random_image();
?>

 

I've been trying to set a max size limit on these images to 200x200 pixels (size ranges from 20x20 pixels to 1024x768), but am unable to get it done.  Everything I've tried results in broken links or no effect.  I'm almost positive that I'm missing something right in front of my eyes, but cannot figure it out.  Any ideas on what I'm doing wrong?

 

Appreciate any help!  ;D

Link to comment
https://forums.phpfreaks.com/topic/72030-solved-need-help-thumbnailing-an-image/
Share on other sites

for the most part it looks ok, can be cleaned up but still ok

 

i'll change the last part to

        if( is_file($dir."/".$random_image) && in_array($ext, $images_ext) )
        {
                readfile($dir."/".$random_image);
        }else{
                readfile($dir."error/error.gif");
        }
        exit;

 

you may have a problem in the

block  switch(strtolower($ext)), as your calling the scrip again, which could, have a never ending effect

ahh nope

 

but heres an basic example that should (your need to update for PNG & GIF) (see the switch's)

        if( is_file($dir."/".$random_image) && in_array($ext, $images_ext) )
        {
                resize($dir."/".$random_image, 100,"jpg");
        }else{
                readfile($dir."error/error.gif");
        }
        exit;

function resize($image, $thumbWidth, $type)
{
      switch($type)
      {
            case "jpg":
                  $img = imagecreatefromjpeg($image);
            break;
      }
      
      $width = imagesx( $img );
      $height = imagesy( $img );

      // calculate thumbnail size
      $new_width = $thumbWidth;
      $new_height = floor( $height * ( $thumbWidth / $width ) );

      // create a new temporary image
      $tmp_img = imagecreatetruecolor( $new_width, $new_height );

      // copy and resize old image into new image
      imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

      switch($type)
      {
            case "jpg":
                  imagejpeg( $tmp_img);
            break;
      }
      
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.