0

So, I have this class that's half-working. Somehow I'm not being able to copy a re-sized sample of the uploaded image, only a black "square" with the "correct" dimensions (screw the dimensions, as long as the thumb comes up clear. one step at the time).

I'm sorry for the WOT but it's driving me cray-cray. Thanks in advance.

    <?php 
    class Upload {
#function from http://stackoverflow.com/a/10666106/587811
public function resize_values($origWidth,$origHeight,$maxWidth = 200,$maxHeight = 200){
    #check for longest side, we'll be seeing that to the max value above
    if($origHeight > $origWidth){ #if height is more than width
        $newWidth = ($maxHeight * $origWidth) / $origHeight;
        $retval = array(width => $newWidth, height => $maxHeight);
    }
    else{
        $newHeight= ($maxWidth * $origHeight) / $origWidth;
        $retval = array(width => $origWidth, height => $newHeight);
}
    return $retval;
}
public function image($picurl, $file, $path="images/uploaded/") {
    echo "function chamada!";
    if ($picurl) {
        $picFileName=rand(1,9999).$_SESSION['id'];
        $picExt=substr(strstr($picurl,'.'),1,3);
        $picExt=strtolower($picExt);
        $allowed = array("jpg","png","gif","bmp");
        if (in_array($picExt,$allowed)) {
            if (getimagesize($file)) {
                $picNewName=str_replace(" ","_",$picFileName.'.'.$picExt);
                $picWhereTo=$path.$picNewName;
                $copy=move_uploaded_file($file, $picWhereTo);       
                if ($copy) {

                    list($width, $height) = getimagesize($picWhereTo);
                    $size = $this->resize_values($width,$height,250,250);
                    $thumb = imagecreatetruecolor($size['width'],$size['height']);
    imagealphablending($thumb, false);
                    $source = imagecreatefromjpeg($picWhereTo);
                    imagecopyresized($thumb,$source,0,0,0,0,$size['width'],$size['height'],$width,$height);
                    $picNewName=$picFileName.'_thumb.'.$picExt;
                    imagejpeg($thumb,$path.$picNewName);

                    $picinfo['where']=$picWhereTo;
                    $picinfo['name']=$picNewName;
                    return $picinfo;
                }
                else return false;
            }
            else return false;
        }
        else return false;
    }
}
    }
    ?>
MoshMage
  • 506
  • 5
  • 30

2 Answers2

1

I've ran into a similar problem like this. This has to do with png's with transparency.

Give this a shot after you create $thumb using imagecreatetruecolor();

imagealphablending($thumb, false);

I'm not entirely certain this is the solution - but I think its along the right track. Your true color supports alpha blending - and it is blending in the background from the jpeg - and it might be confused by the lack of information.

If this doesn't work, please describe the exact image format you are uploading so we can try it out and see what happens.

Aaron Saray
  • 1,178
  • 6
  • 19
  • Sadly this isn't the culprit; I'm trying to upload a very simple image, 2 red squares on a white background (made in paint) for testing proposes. I have tried with jpg, png, bmp and real images. I even thought i didn't had GD so i `phpinfo'ed` and it said i do have it. – MoshMage Feb 18 '13 at 18:50
  • Have you tried substituting in imagecopyresampled() instead? I wonder if that is the culprit, as you say ;) Keep commenting - lets figure this out. – Aaron Saray Feb 18 '13 at 19:50
  • I think i have tried "all" imagecreates I could find. I'm thinking that the problem might be a "the file is not anywhere" when i call the createimage function. Sounds weird, i know, but I can't find another explanation as using `TimThumb.php` works and i have dabbled on the code and it has the same operations as mine [well, more featured, but still.] – MoshMage Feb 19 '13 at 15:10
1

Change your

$source = imagecreatefromjpeg($file);

to

$source = imagecreatefromjpeg($picWhereTo);

And this is how you call the function

$objoflclass->image($_FILES['img']['name'],$_FILES['img']['tmp_name'],'');

where $_FILES['img'] is the name of the image upload field and i believe from this u can understand what was the problem

zamil
  • 1,920
  • 4
  • 17
  • 32
  • I had previously used `$source = $picWhereTo` but had the same result, a blank image. And yes, `if ($picinfo = $upload->image($_FILES['image']['name'],$_FILES['image']['tmp_name']))` is how I am calling it; I cannot understand. As a temp. fix i got `TimThumb.php` making the calls - will get back to it once i have "time" – MoshMage Feb 19 '13 at 15:05
  • i tested it to success,then only i suggested you the changes.It should work. – zamil Feb 19 '13 at 16:02
  • Then I'll make sure to re-test soon as i have the chance. – MoshMage Feb 20 '13 at 12:21