在页面上显示php生成的图像

Ogu*_*nwu 2 php image-manipulation image image-processing

我有一个用于修改我的图像的功能,我想在我的页面上显示这些图像以及其他页面内容.

PHP图像功能:

<?php

 function dynamicImage($nw, $nh, $source, $stype, $dest) {
        $size = getimagesize($source);
        $w = $size[0];
        $h = $size[1];
        switch($stype) {
            case 'gif':
            $simg = imagecreatefromgif($source);
            break;
            case 'jpg':
            $simg = imagecreatefromjpeg($source);
            break;
            case 'png':
            $simg = imagecreatefrompng($source);
            break;
        }
        $dimg = imagecreatetruecolor($nw, $nh);
        $wm = $w/$nw;
        $hm = $h/$nh;
        $h_height = $nh/2;
        $w_height = $nw/2;
        if($w> $h) {
            $adjusted_width = $w / $hm;
            $half_width = $adjusted_width / 2;
            $int_width = $half_width - $w_height;
            imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
        } elseif(($w <$h) || ($w == $h)) {
            $adjusted_height = $h / $wm;
            $half_height = $adjusted_height / 2;
            $int_height = $half_height - $h_height;
            imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
        } else {
            imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
        }
        header( "Content-type: image/jpeg" );
        imagejpeg( $dimg );
        imagedestroy( $dimg );
        //imagejpeg($dimg,$dest,100);
    }
?>
Run Code Online (Sandbox Code Playgroud)

在我希望它显示的PHP页面上(我调用dynamicImage函数):

<img id="profilepic" src="<?php $size=getimagesize($pic); $imgx=240; $imgy=($size[1]*$imgx)/$size[0]; dynamicImage($imgx, $imgy, $pic, substr($pic, -3), ''); ?>"  />
Run Code Online (Sandbox Code Playgroud)

但它最终给出了一个空白页面.

我如何使用基本编码,因为我需要支持各种旧版浏览器.

在此先感谢(这个网站已经非常有帮助的微笑).

tho*_*alt 5

不要直接在图片标记中调用php代码

假设您创建了一个用于生成图像并命名的脚本

/myimage.php
Run Code Online (Sandbox Code Playgroud)

码:

<?php

$type = $_GET['type'] || 'png';
// remember to test if variables exist and handle gracefully
$mimetypes = array(
    'jpg' => 'image/jpeg',
    'png' => 'image/png',
    // ad nauseum
);

header('Content-type: '.$mimetypes[$type]);

// handle parsing and output of image here.   
Run Code Online (Sandbox Code Playgroud)

然后你在你的页面中使用它像这样:

<img src="/myimage.php?width=240&height=240&type=jpg" />
Run Code Online (Sandbox Code Playgroud)