本文介绍了在页面上显示php生成的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a function i use in modifying my images, and i want to display thos images on my page alongside other page content.

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);
    }
?>

在我想要显示它的php页面上(我称为dynamicImage函数):

on php page where i want it displayed ( i call the dynamicImage function ) :

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

但最终会给出空白页.

由于我需要支持各种较旧的浏览器,因此如何使用基本编码进行处理.

How do i go about it witout using base encode since i need to support a wide range of older browsers.

预先感谢(该站点对 微笑 很有帮助).

Thanks in advance (this site has been so helpful smiles).

推荐答案

不要在您的图片标签中直接调用php代码

Don't call the php code directly in your image tag

让我们说您创建了一个用于生成图像的脚本并将其命名

lets say you create a script for generating an image and name it

/myimage.php

代码:

<?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.

然后您可以像这样在页面中使用它:

you then use it in your page like this:

<img src="/myimage.php?width=240&height=240&type=jpg" />

这篇关于在页面上显示php生成的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 08:22