2012-12-17 21 views
0

我有一个外部PHP文件,可以进行一些图片大小调整(image.php)。每次使用'图片'标签时,我都需要调用它,以便可以填充“src”。我如何告诉HTML文件使用外部PHP页面,以及如何在处理'image'“src”标签时使用它?如何在HTML图片标签中使用外部文件中的PHP函数?

这是即时通讯使用尝试显示图像。

我是否需要在我的'头部'部分包含一些东西?

感谢您的帮助!

<li><a href= "/intPics//1.jpg"> <img src="<?php loadImage('/intPics//1.jpg', 300,300) ?>"<div>1.jpg</div></a></li> 

这是PHP文件

<?php 



    function imageResizer($url, $width, $height) { 



       header('Content-type: image/jpeg'); 



       list($width_orig, $height_orig) = getimagesize($url); 



       $ratio_orig = $width_orig/$height_orig; 



       if ($width/$height > $ratio_orig) { 

        $width = $height*$ratio_orig; 

       } else { 

        $height = $width/$ratio_orig; 

       } 



       // This resamples the image 

       $image_p = imagecreatetruecolor($width, $height); 

       $image = imagecreatefromjpeg($url); 

       imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height,  $width_orig, $height_orig); 



       // Output the image 

       imagejpeg($image_p, null, 100); 



     } 



     / /works with both POST and GET 

     $method = $_SERVER['REQUEST_METHOD']; 



     if ($method == 'GET') { 



       imageResize($_GET['url'], $_GET['w'], $_GET['h']); 



     } elseif ($method == 'POST') { 



      imageResize($_POST['url'], $_POST['w'], $_POST['h']); 

     } 



     // makes the process simpler 

     function loadImage($url, $width, $height){ 

     echo 'image.php?url=', urlencode($url) , 

     '&w=',$width, 

     '&h=',$height; 

     } 



?> 

回答

2

我不知道如果我理解正确你的问题,但我会尽力回答:

你可以调用PHP脚本直接从src在你的形象标签

<img src="image.php" /> 

和你的PHP文件,你会设置内容类型为图像格式

<?php header('Content-Type:image/jpeg'); ?> 

这样php脚本返回一个图像。你甚至可以通过参数,例如image.php?width=200&height=100到php脚本。

+0

我发布了一些代码,也许可以帮助澄清,对不起,很难理解。我不太确定如何说出我的问题。 – bsqrd7

0

我不知道你在你的PHP文件,但

<?php require_once('path/image.php') ?> 

应该导入它,那么你只需要使用的功能并通过PHP脚本填充src?

编辑:如果你给我进一步的信息,p.e.代码,我可以帮助你在细节方面提出进一步的问题。

+0

我刚刚在原始问题中发布了代码,谢谢! – bsqrd7

+0

这就是我想要做的... http://www.script-tutorials.com/how-to-resize-images-on-server-side/ – bsqrd7

相关问题