Jump to content

Trouble Trying to Generate a Thumbnail Image?


brent123456

Recommended Posts

I am trying to make a thumbnail image but I keep getting all these errors.

 

Warning: imagesx(): supplied argument is not a valid Image resource in C:\myxampp\xampp\htdocs\seedguys\site_pages\includes\processaddseeds.php on line 147

 

Warning: imagesy(): supplied argument is not a valid Image resource in C:\myxampp\xampp\htdocs\seedguys\site_pages\includes\processaddseeds.php on line 148

 

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in C:\myxampp\xampp\htdocs\seedguys\site_pages\includes\processaddseeds.php on line 163

 

This is the code that I am using to make the thumbs, I have GD install and working I ran this script to test..

<?php
  if (function_exists('ImageCreateTrueColor')) {
      echo 'ok you already have GD';
  }else{
      echo 'sorry, it seems that GD is not installed';
  }
?>

 

It is saying that it is ok and I have GD.

 

Here is my code

if (is_uploaded_file($_FILES['image']['tmp_name'])) {
            (list($name, $ext) = explode('.', $_FILES['image']['name'])); // get the image ext
            $allowed_extensions = array("gif", "jpg", "jpe", "jpeg");
            if (!in_array($ext, $allowed_extensions)) {
                print 'This file is no allow for upload, your seeds were added by file was not uploaded';
                $i = '';
            }else{
            $image_name = md5(uniqid(rand())).".$ext";  // build the new file name
            if (move_uploaded_file($_FILES['image']['tmp_name'],
                "includes/uploaded_images/seed_images/$image_name")) { // move file here

                        echo 'The file has been uploaded!';
                        $image_name = 'includes/uploaded_images/seed_images/' . $image_name;
                        $thumb_name = $image_name;
                        $size = '100';
                        createthumb($image_name, $thumb_name ,$size,$size);
                        
                        } else { //the file couldn't be moved
                        
                            echo '<div class="error">The file could not be moved.</div>';
                            $i = '';
                            }
                             $i =  $image_name;
            }
                        } else {
                            $i = '';
                            }
        

 

here is the function...

 

function createthumb($name,$filename,$new_w,$new_h){
    $system=explode('.',$name);
    if (preg_match('/jpg|jpeg/',$system[1])){
        $src_img=imagecreatefromjpeg($name);
    }
    if (preg_match('/png/',$system[1])){
        $src_img=imagecreatefrompng($name);
    }
    
     if (preg_match('/gif/',$system[2])){
        $src_img=imagecreatefromgif($name);
    }    
    
    
       $old_x=imagesx($src_img);
       $old_y=imagesy($src_img);
            if ($old_x > $old_y) {
                $thumb_w=$new_w;
                $thumb_h=$old_y*($new_h/$old_x);
                }
                if ($old_x < $old_y) {
                    $thumb_w=$old_x*($new_w/$old_y);
                    $thumb_h=$new_h;
                }
                    if ($old_x == $old_y) {
                        $thumb_w=$new_w;
                        $thumb_h=$new_h;
                        }
                        
                        $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
                        imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
                        
                        if (preg_match("/png/",$system[1]))
                        {
                            imagepng($dst_img,$filename); 
                        } else {
                            imagejpeg($dst_img,$filename); 
                        }
                            createthumb($dst_img ,'includes/uploaded_images/' . $dst_img ,100,100); 
                            imagedestroy($dst_img); 
                            imagedestroy($src_img);
                            
                            if (preg_match("/gif/",$system[2]))
                        {
                            imagegif($dst_img,$filename); 
                        } else {
                            imagejpeg($dst_img,$filename); 
                        }
                            createthumb($dst_img ,'includes/uploaded_images/' . $dst_img ,100,100);
                            imagedestroy($dst_img); 
                            imagedestroy($src_img); 
                        }
                        

 

I hope someone can help me out with this.. Thanks

Link to comment
Share on other sites

Hmm. Ever consider that none of the matches succeeded? Or maybe one did, but the image was never created/loaded?  Try testing $src_img to see if it is set before the imagesx/sy, see what happens. Or do print out something when and/or if an image is created. :S If not, then guess I'd need to look again.. heh.

Also, I'm confused as to why you use $system[1] for the first 2 file-ext types but $system[2] for the gif?

 

???

Link to comment
Share on other sites

function createthumb($name,$filename,$new_w,$new_h){
    $system=explode('.',$name);
    if (preg_match('/jpg|jpeg/',$system[1])){
        $src_img=imagecreatefromjpeg($name);
    }
    if (preg_match('/png/',$system[1])){
        $src_img=imagecreatefrompng($name);
    }
    
     if (preg_match('/gif/',$system[1])){
        $src_img=imagecreatefromgif($name);
    }
        
     echo $src_img;
     if ($src_img) {
       $old_x=imagesx($src_img);
       $old_y=imagesy($src_img);
            if ($old_x > $old_y) {
                $thumb_w=$new_w;
                $thumb_h=$old_y*($new_h/$old_x);
                }
                if ($old_x < $old_y) {
                    $thumb_w=$old_x*($new_w/$old_y);
                    $thumb_h=$new_h;
                }
                    if ($old_x == $old_y) {
                        $thumb_w=$new_w;
                        $thumb_h=$new_h;
                        }
                        
                        $dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
                        imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 
                        echo $dst_img;
                        if (preg_match("/png/",$system[1]))
                        {
                            imagepng($dst_img,$filename); 
                        } else {
                            imagejpeg('includes/uploaded_images/seed_images/thumbs/' . $dst_img,$filename); 
                        }
                            createthumb('includes/uploaded_images/' . $dst_img ,'includes/uploaded_images/seed_images/thumbs/' . $dst_img ,100,100); 
                             
                            
                            
                            if (preg_match("/gif/",$system[1]))
                        {
                            imagegif($dst_img,$filename); 
                        } else {
                            imagejpeg('includes/uploaded_images/seed_images/thumbs/' . $dst_img,$filename); 
                        }
                            createthumb('includes/uploaded_images/' . $dst_img ,'includes/uploaded_images/seed_images/thumbs/' . $dst_img ,100,100);
                             
                             
                        }

 

I added in an if statement to check the $scr_img and it now makes a thumbnail but it is overwriting the original image with the thumb. Is there anyway to change the thumb directory so i doesn't overwrite the main image file? All I want to do is to create a thumbnail from the original in a new directory and keep the original one in the same folder. I wish I knew how to get this.

Link to comment
Share on other sites

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.