php resize image
- by haohan
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'); , 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.