开发者

PHP checking function output

开发者 https://www.devze.com 2023-03-16 23:25 出处:网络
I have a basic function to resize image given below: function resize() { ..... ... .. imagejpeg($image_new,$img_path,80);

I have a basic function to resize image given below:

function resize() {

.....
...
..

imagejpeg($image_new,$img_path,80);

imagedestroy($image_old);
imagedestroy($image_new);
}

I need to call it in another function and see its output. How can i d开发者_开发百科o that? I want something like:

function test(){
//calling resize function
if (resize() === true){ 
    echo "Success";
    } else {
    echo "resized failed"; 
    }
}

What I'm not understanding is that the resize function simply creates the image and detroys the older image files. How it will return the output that it was successful or not? Thanks.


As written your resize function is not returning information on whether it succeeds or fails and, furthermore, it always destroys both images (the two imagedestroy calls). You'll need to rewrite the function to take account of the return codes from imagedestroy and imagejpeg. The documentation for the latter indicates that that function will return a boolean indicating whether it was successful or not.


imagejpeg() returns a boolean FALSE on failure (according to the API; collect its return and use that as the return of your resize() function:

function resize () {
  ...
  $success = imagejpeg($image_new, $img_path, 80);
  ...
  return $success
}

Also, you should probably be passing in the image as a parameter to resize()... it looks like you have a few global variables, which isn't good(1) practise(2)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号