Hi frnds...

here i am getting some error...plz check this one...
these are the two files regarding my code...

thumbnail.php

<?php
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 (strtoupper($this->img["format"])=="JPG" || strtoupper($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);
        } elseif ($this->img["format"]=="GIF") {
            //GIF
            $this->img["format"]="GIF";
            $this->img["src"] = @ImageCreateFromGIF ($imgfile);
        } elseif ($this->img["format"]=="WBMP") {
            //WBMP
            $this->img["format"]="WBMP";
            $this->img["src"] = @ImageCreateFromWBMP ($imgfile);
        } else {
        return 0;
        }
        $this->img["src_width"] = imagesx($this->img["src"]);
        $this->img["src_height"] = imagesy($this->img["src"]);
        
    }
/*$save => containa a path where the thumbnail will be stored ex. sohail/a.png
  $w => width of thumbnail    
  $h => height of thumbnail
*/
    function saveImage($save="",$w,$h)
    {
        
            $old_x=imageSX($save);
            $old_y=imageSY($save);
            if ($old_x > $old_y) {
                $thumb_w=$w;
                $thumb_h=$old_y*($h/$old_x);
            }
            if ($old_x < $old_y) {
                $thumb_w=$old_x*($w/$old_y);
                $thumb_h=$h;
            }
            if ($old_x == $old_y) {
                $thumb_w=$w;
                $thumb_h=$h;
            }
        
        
        $this->img["dest_width"] = $thumb_w ;
        $this->img["dest_height"] = $thumb_h ;
        
        /* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function*/
        $this->img["des"] = imagecreatetruecolor($this->img["dest_width"],$this->img["dest_height"]) ;
       
        @imagecopyresized($this->img["des"], $this->img["src"], 0, 0, 0, 0, $this->img["dest_width"], $this->img["dest_height"], $this->img["src_width"], $this->img["src_height"]);

        if ($this->img["format"]=="JPG" || $this->img["format"]=="JPEG") {
            //JPEG
            

            
            @imagejpeg($this->img["des"],"$save",75);
        } elseif ($this->img["format"]=="PNG") {
            //PNG
            @imagepng($this->img["des"],"$save");
        } elseif ($this->img["format"]=="GIF") {
            //GIF
            @imagegif($this->img["des"],"$save");
        } elseif ($this->img["format"]=="WBMP") {
            //WBMP
            @imagewbmp($this->img["des"],"$save");
        }
    }
}

//End of thumbnail

gallery.php

@$tmp_name0 = $_FILES["attachments4"]["tmp_name"][0];
        @$name0 = $_FILES["attachments4"]["name"][0];
	@$type0=$_FILES["attachments4"]["type"][0];
	 @$size0=$_FILES["attachments4"]["size"][0];

          $title1="../../gallery/allgallerys/$gcategory/$gname";
	
	@move_uploaded_file($tmp_name0, "$title1"."/"."$name0");

if($name0!='')
{	

$thumbpath0="$tpath/thumb/$name0";
$filepath0="$title1/$name0";
$thumb = new thumbnail($filepath0);
[B]$thumb -> saveImage($thumbpath0,"115","120");[/B]
$path="$title"."/"."$name0";
$sql="insert into galleryphotos values('','$gid','$path','$thumbpath0','$date')";
mysql_query($sql)or die(mysql_error());
}

here everything is ok..but my problem is getting warning
and also image is damaged....plz rectify this problem...

Dani AI

Generated

Short answer: imagesx()/imagesy() are failing because they received FALSE instead of an image resource. That happens when the source wasn't loaded (imagecreatefrom* returned false) or when the code accidentally calls imagesx/imagesy on a filename (the thumbnail path) instead of the source resource. was right to point at the lines that measure the image; 's reminder to open the file first is also relevant — but the real fixes are: verify the source file exists/is readable, stop calling imagesx/imagesy on the destination filename, and stop suppressing errors with @ while you debug.

Concrete steps to fix and harden the class

  • Check move_uploaded_file() return and use file_exists()/is_readable() on the path you pass into the constructor. Echo or log the full realpath() while debugging.
  • Remove the @ error suppressors so you see the real failures from imagecreatefrom*.
  • In saveImage() use the already-loaded source dimensions or the source resource, not the $save string. Compute the scale with a min-ratio to preserve aspect ratio.
  • Use imagecopyresampled() (better quality) and preserve PNG/GIF transparency (imagealphablending/imagesavealpha or GIF transparent index).
  • Ensure the thumbnail directory exists and is writable before saving.

Minimal example (replace the bad imageSX/imageSY usage)

// inside saveImage()
if (empty($this->img['src'])) {
  trigger_error('Source image not loaded', E_USER_WARNING);
  return false;
}
$srcW = $this->img['src_width'];
$srcH = $this->img['src_height'];
$ratio = min($w / $srcW, $h / $srcH);
$dstW = max(1, (int)($srcW * $ratio));
$dstH = max(1, (int)($srcH * $ratio));
// create dest, preserve transparency if needed, then:
imagecopyresampled($dst, $this->img['src'], 0,0,0,0,$dstW,$dstH,$srcW,$srcH);

Quick checklist while debugging: verify paths with realpath(), check permissions, inspect return values (move_uploaded_file, imagecreatefrom*), var_dump the source resource, and imagedestroy() when done. Fixing those will remove the warnings and stop producing broken-looking thumbnails.

Recommended Answers

All 5 Replies

$thumb -> saveImage($thumbpath0,"115","120");

If your refering to the above line being the error line then it would be because you need to specify $recourse in the first parameter and not $path. So try looking up functions like createimagefromgif() for the first parameter input. Basically functions like createimagefromjpeg() load a file into the php file system where it can be used via variables.

Hello cwarn,

thanks 4 ur reply..

I dont know exactly where the error occured..

the errors are:

Warning: imagesx(): supplied argument is not a valid Image resource in C:\wamp\www\telugu\admin\gallery\thumbnail.php on line 42

Warning: imagesy(): supplied argument is not a valid Image resource in C:\wamp\www\telugu\admin\gallery\thumbnail.php on line 43

what is meant by $recourse..plz explian this and check the errors which i phased..

$thumb -> saveImage($thumbpath0,"115","120");

If your refering to the above line being the error line then it would be because you need to specify $recourse in the first parameter and not $path. So try looking up functions like createimagefromgif() for the first parameter input. Basically functions like createimagefromjpeg() load a file into the php file system where it can be used via variables.

The error report makes it so much easier. You can ignore most of what I said on my previous post as according to the error message it is the following two lines.

$old_x=imageSX($save);
            $old_y=imageSY($save);

if for example the save image is a jpeg then replace with the following.

$resource=imagecreatefromjpeg($save);
$old_x=imageSX($resource);
            $old_y=imageSY($resource);

Hello sir,

Sorry for not getting again...i done what u said perfectly..but again i am getting an extra error as

Warning: imagecreatefromjpeg(../../gallery/allgallerys/Heroines/Naina/thumb/reema-sen-4-51247033438.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in C:\wamp\www\telugu\admin\gallery\thumbnail.php on line 41

and also previous errors are repeated..but, the thumbnail is creating. ..but not good clarity..image lokking damaged...

Have you had any experience with PHP files?

What you do is first OPEN the file. This allows PHP to read the contents and attributes it contains.

This is what you must do with your image. You need to use the functions stated above by cwarn23 depending on the file extension.

These will open the files and allow PHP to read information on the specified file. Without this your function will not work as PHP can change the information without knowing everything about this file.

createimagefromjpeg( $path_to_jpeg );

createimagefromgif( $path_to_gif );

createimagefrompng( $path_to_png );

/* I'm not 100% sure but I think there is a createimagefromjpg() function
but don't quote me on it. */
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.