简体   繁体   English

我在PHP中有一个非常奇怪的函数returnvalue行为

[英]I got a very strange function returnvalue behaviour in PHP here

Hi I have a very strange behaviour of PHP and hope that you are able to help me. 嗨,我的PHP行为很奇怪,希望您能对我有所帮助。

I have a Symfony 1.4 based PHP project on PHP 5.4.7. 我在PHP 5.4.7上有一个基于Symfony 1.4的PHP项目。

I have the following class function to resize images: 我有以下类函数来调整图像大小:

public function getResizedImageHorizontal($newWidth) {
    $image = null;
    $data = $this->getData();

    if(strlen($data["image"]) > 0) {
        $image = imagecreatefromstring($data["image"]);
        $newHeight = intval(imagesy($image) / 100) * ($newWidth / (imagesx($image) / 100));
        $newImage = imagecreatetruecolor($newWidth, $newHeight);
        imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, imagesx($image), imagesy($image));
        ob_start();
        imagejpeg($newImage, null, 100);
        $image = ob_get_contents();
        ob_end_clean();
    }

    var_dump($image);
    return $image;
}

and this is my function call in the template: 这是模板中的函数调用:

$image = $galleryObj->getResizedImageHorizontal(100);
var_dump($image);

Because I get no result from my function (which is working perfectly when I put the code directly in the template) I put 2 vardumps in the codepieces above to explain it more clear: 因为我没有从函数中得到任何结果(将代码直接放在模板中时效果很好),所以我在上面的代码段中放入了两个vardumps来更清楚地解释它:

The first vardump returns me the imagecontent resized (source is blob in the db) as intended, the second one in the template as direct call, has an empty result. 第一个vardump向我返回了按预期调整大小的imagecontent(源在db中为blob),第二个在模板中作为直接调用返回了空结果。

Or in short: directly before the "return" the value is correct: string(9474) "... 或简而言之:直接在“返回”值之前是正确的:string(9474)“ ...

But in my template directly after the function call immediately: string(0) "" 但是在我的模板中,直接在函数调用之后:string(0)“”

What the heck is going on here, did I find some bug in PHP or is it known in combination with Symfony / Doctrine 1.4 by executing stuff from the template? 到底发生了什么,我是否发现了PHP中的一些错误,或者通过执行模板中的内容与Symfony / Doctrine 1.4相结合而闻名?

I cannot get this value passed which is very odd, it is unlikely but maybe someone had the same trouble as me so I thought I should ask here. 我无法通过这个值,这很奇怪,这不太可能,但是也许有人遇到了与我相同的麻烦,所以我认为我应该在这里提出。

Thanks in advance~ 提前谢谢〜

I put this problem for a longer while aside because I had something else to do, I now reencountered the problem, found my old post and the comment by Michal Trojanowski perfectly solved it, thanks! 我把这个问题搁置了一段时间,因为我还有其他事情要做,现在我遇到了这个问题,发现了我的旧帖子,Michal Trojanowski的评论很好地解决了这个问题,谢谢!

Here is the solution once more: 这里再次是解决方案:

$image = $galleryObj->getRawValue()->getResizedImageHorizontal(100); $ image = $ galleryObj-> getRawValue()-> getResizedImageHorizo​​ntal(100);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM