Hi

I have searched this forum quite a bit and have read most of the posts regarding resizing of images, jpg's to be exact. I have tried loads of the posted codes that appear to have worked for other people but I cannot get them to work so I am hoping someone can read this a possibly post some code that will work.

My PHP details are v4.3.1 with GD Enabled:

  • GD Version : bundled (2.0 compatible)

  • FreeType Support: enabled

  • FreeType Linkage: with freetype

  • GIF Read Support: enabled

  • JPG Support: enabled

  • PNG Support: enabled

  • WBMP Support: enabled

The reason I have posted that is because I thought it might be important cause reading the posts on this site there are differences between GD v1.6 & 2.0 so hopefully that will give you enough info to use the appropriate code I would require.

Basically I have an image Gallery that is offsite [ie. not stored on my domain hosting package], it is stored on a free isp site that offers free space. I run all my code on my hosting site and just reference the images from there, the code I use to select a random image is:

$Q04 = "SELECT gp_site, gp_pic, gp_format, gp_who
		FROM jag_gallery_photos
		WHERE gp_rank <= '$userinfo[user_photo_rank]'
		ORDER BY rand()";
$R04 =  mysql_query($Q04) or die("Bad Q04:".mysql_error());
list($gp_site, $gp_pic, $gp_format, $gp_who, $gp_rank) = sql_fetch_row($R04);

echo "<IMG SRC='http://$gp_site/$gp_pic.$gp_format'
BORDER='0' WIDTH='150' HEIGHT='113' ALT='$gp_who'>\n";

This works fine, the only thing is the images are 600 X 450 and I have just used the HTML tags to resize the random image. I want to show this image on the home page at 150 X 113, as you know this still takes the same amount of time to load up as it would if you were looking at it at 600 X 450, hence why I wanted to temporarily resize and show the image on the home page.

Any help would be appreciated as I have been trying to figure this out for a bit now and am at a loss.

Thanks in advance

    This might help. When you have an image on the server, you can resize it then also have a link to enlarge it.

    This is the class: Needs to be uploaded into the images folder.

    
    class thumbnail
    {
    	var $img;
    
    function thumbnail($imgfile)
    {
    	//detect image format
    	$this->img["format"]=ereg_replace(".*\.(.*)$","\\1",$imgfile);
    	$this->img["format"]=strtoupper($this->img["format"]);
    	if ($this->img["format"]=="JPG" || $this->img["format"]=="JPEG") {
    		//JPEG
    		$this->img["format"]="JPEG";
    		$this->img["src"] = ImageCreateFromJPEG ($imgfile);
    	} elseif ($this->img["format"]=="PNG") {
    		//PNG
    		$this->img["format"]="PNG";
    		$this->img["src"] = ImageCreateFromPNG ($imgfile);
    	} else {
    		//DEFAULT
    		echo "Not Supported File";
    		exit();
    	}
    	@$this->img["lebar"] = imagesx($this->img["src"]);
    	@$this->img["tinggi"] = imagesy($this->img["src"]);
    	//default quality jpeg
    	$this->img["quality"]=75;
    }
    
    function size_auto($size=100)
    {
    	//size
    	if ($this->img["lebar"]>=$this->img["tinggi"]) {
    		$this->img["lebar_thumb"]=$size;
    		@$this->img["tinggi_thumb"] = ($this->img["lebar_thumb"]/$this->img["lebar"])*$this->img["tinggi"];
    	} else {
        	$this->img["tinggi_thumb"]=$size;
    		@$this->img["lebar_thumb"] = ($this->img["tinggi_thumb"]/$this->img["tinggi"])*$this->img["lebar"];
    	}
    }
    
    function jpeg_quality($quality=75)
    {
    	//jpeg quality
    	$this->img["quality"]=$quality;
    }
    
    function show()
    {
    	//show thumbnail
    	@Header("Content-Type: image/".$this->img["format"]);
    
    	$this->img["des"] = ImageCreate($this->img["lebar_thumb"],$this->img["tinggi_thumb"]);
    		@imagecopyresized ($this->img["des"], $this->img["src"], 0, 0, 0, 0, $this->img["lebar_thumb"], $this->img["tinggi_thumb"], $this->img["lebar"], $this->img["tinggi"]);
    
    	if ($this->img["format"]=="JPG" || $this->img["format"]=="JPEG") {
    		//JPEG
    		imageJPEG($this->img["des"],"",$this->img["quality"]);
    	} elseif ($this->img["format"]=="PNG") {
    		//PNG
    		imagePNG($this->img["des"]);
    	} 
    }
    
    function save($save="")
    {
    	//save thumbnail
    	if (empty($save)) $save=strtolower("./thumb.".$this->img["format"]);
    	$this->img["des"] = ImageCreate($this->img["lebar_thumb"],$this->img["tinggi_thumb"]);
    		@imagecopyresized ($this->img["des"], $this->img["src"], 0, 0, 0, 0, $this->img["lebar_thumb"], $this->img["tinggi_thumb"], $this->img["lebar"], $this->img["tinggi"]);
    
    	if ($this->img["format"]=="JPG" || $this->img["format"]=="JPEG") {
    		//JPEG
    		imageJPEG($this->img["des"],"$save",$this->img["quality"]);
    	} elseif ($this->img["format"]=="PNG") {
    		//PNG
    		imagePNG($this->img["des"],"$save");
    	} 
    }
    }
    
    

    Then use this to call it:

    <?
    
    //Full path of your site directory
    $site_dir = "http://www.yourdomain.com";
    
    //How you want to call the image variable is up to you.
    
    if (!file_exists("$site_dir/images/" . "thumb_$image")){
    				                             $thumb=new thumbnail("$site_dir/images/$image");
    				                             $thumb->size_auto(150);	
    				                             $thumb->jpeg_quality(100);
    				                             $thumb->save("$site_dir/images/thumb_$image");}?>
    

    Then you can do something like this:

    <img src='images/thumb_$image'><br><a href='images/$image' target='_blank'>Enlarge Image</a>
    

      Thanks Very much for you reply, I just have a few questions if thats alright

      The files are stored as i said on antother server to the code and they are stored in something like the following

      images/0011-000/
      images/0025-005/

      and so on, each member has a folder and posiably sub folder in which the images are stored, would that mean i have to save the class file into each directory plus the server they are stored on doesnt have PHP, so is there another way of doing that bit

      Also when you say its a "class" how would i save that file would it be blahblah.class.

      Sorry to be anoying I am not an amature i just have never used them before or GD for that mater.

      Thanks for your quick reply and if ya know a way of doing it without having to load up the class into each image director (casue there is no PHP) that would be super

      Thanks again

      P.S Just incase you wanted to see what i was tlaking about - www.jagamarog.com

        Ok, now it's getting a little confusing. First of all, to save the class, just save it into a text file: makethumb.txt Make sure when you save it as a txt file, to not delete the <? ?> tags from the script.

        I am not sure how to go about the rest because you will have to store the makethumb.txt file in the images folder along with the images. You should be able to place this code into the main page.

        <?
        
        //Full path of your site directory
        $site_dir = "http://www.yourdomain.com";
        
        //How you want to call the image variable is up to you.
        
        if (!file_exists("$site_dir/images/" . "thumb_$image")){
                                                     $thumb=new thumbnail("$site_dir/images/$image");
                                                     $thumb->size_auto(150);    
        $thumb->jpeg_quality(100); $thumb->save("$site_dir/images/thumb_$image");}?>

        Then set the $site_dir to the site that stores the pics. What it should then do is check to see if thumb_$image exists on the secondary server and if not to make one out of the coresponding $image. Then it should save a new thumbnail of that image to the secondary server. Then you should be able to call it like this.

        <img src='http://www.yoursecondaryserver.com/images/thumb$image'><br><a href='http://www.yoursecondaryserver.com/images/$image' target='blank'>Enlarge Image</a>

        I'm not sure if all I stated will work or not. If you would like a preview of what this does go HERE You will see an image of a gun. That image is usually 550 x 337 in size. It made an automatic thumbnail of that image to 75 x 45, then displays it for faster loading. It then will place a link to the original file (the big one) in a new window.

        -Blake

          Once again thank you very much for your reply the script is great, I actually haven’t tried it yet, will do when I get home but am sure it will do what I need.

          Thanks for explaining that class thing too.

          One last and final question for ya, does it have to save the image, can it not just resize it show it on the screen and then get rid of it?

            No, because when you call an image it has to have a src from somewhere. So it has to save it into a folder then show it from the web folder. If you played around with it a little bit, I'm sure you could get something written up to delete the image as well.

            Do something like this:

            if (file_exists("$site_dir/images/" . "thumb_$image"))
            {
            //your deleting script here
            }
            

            -Blake

            P.S. In the folder that stores the images on the secondary server, you must give it write permissions.

              Write a Reply...