0

I have a class to read and output the image content, if $width is set, it will resize the image, and then output it.

If I call the function like this $image->readImage('123.jpg'); , it can output the image file correctly, but when I call $image->readImage('123.jpg', 300); to resize it, it just display a black image with resized width & height.

And I tried to replace the code from

@imagejpeg($thumb, null, 100);

to

@imagejpeg($image, null, 100);

will works~

-

protected function readImage($fileName, $width = 0) 
{
    if ($width <= 0) {
        return @file_get_contents($this->destination . '/' . $fileName);
    } else {
        $imageSize = @getimagesize($this->destination . '/' . $fileName);
        $actualWidth = $imageSize[0];
        $actualHeigth = $imageSize[1];

        if ($actualWidth <= $width) {
            return @file_get_contents($this->destination . '/' . $fileName);
        }
        $height = (100 / ($actualWidth / $width)) * .01;
        $height = @round($actualHeigth * $height);

        $image = @imagecreatefromjpeg($this->destination . '/' . $fileName);
        $thumb = @imagecreatetruecolor($width, $height);
        @imagecopyresampled($thumb, $image, 0, 0, 0, 0, $width, $height, $actualWidth, $actualHeight);

        ob_start();
        @imagejpeg($thumb, null, 100);
        $bits = ob_get_contents();
        ob_end_clean();

        return $bits;
    }
}

Any experts know what happened and help me to solve it ?

Thanks.

4
  • 1
    If you removed all the @ symbols from in front of your function calls, you'd probably get a decent error message that might give you a clue. Using @ is a bad habit, unless you really really need it. Commented Mar 27, 2010 at 0:08
  • Your first paragraph is a little bit confusing since you claim that the same exact code does two different things...could you clarify what you mean when you say you call $image->readImage('123.jpg');? Commented Mar 27, 2010 at 0:29
  • I found that problem, my typo error, $actualHeight and $actualHeigth. Sorry. Commented Mar 27, 2010 at 0:40
  • Ha ha, I spent 30 minutes one time trying to figure out an error where it told me the line number! After 30 minutes, my friend finally looked over and said, "You misspelled length...". I had spelled it "lenth" and couldn't figure it out... Commented Mar 27, 2010 at 0:54

1 Answer 1

8

you've been inconsistant in your spelling of $actualHeight vs $actualHeigth

if you didn't have so many @ everywhere, then php would have told you this.

Sign up to request clarification or add additional context in comments.

1 Comment

If you don't need them why are they there haohan?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.