Php 我的缩略图功能是';t工作不正常 当这个代码运行时,浏览器屏幕上会有一个灰色和白色方格形的黑色,当它在窗体上的表格单元格中显示缩略图时, function createThumb( $imageUrl, $thumbWidth ) { // load image and get image size $img = imagecreatefromjpeg( $imageUrl ); $width = imagesx( $img ); $height = imagesy( $img ); // calculate thumbnail size $new_width = $thumbWidth; $new_height = floor( $height * ( $thumbWidth / $width ) ); // create a new temporary image $tmp_img = imagecreatetruecolor( $new_width, $new_height ); // copy and resize old image into new image imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height ); // display thumbnail header("Content-type:image/jpeg"); imagejpeg( $tmp_img,$imageUrl,80); };

Php 我的缩略图功能是';t工作不正常 当这个代码运行时,浏览器屏幕上会有一个灰色和白色方格形的黑色,当它在窗体上的表格单元格中显示缩略图时, function createThumb( $imageUrl, $thumbWidth ) { // load image and get image size $img = imagecreatefromjpeg( $imageUrl ); $width = imagesx( $img ); $height = imagesy( $img ); // calculate thumbnail size $new_width = $thumbWidth; $new_height = floor( $height * ( $thumbWidth / $width ) ); // create a new temporary image $tmp_img = imagecreatetruecolor( $new_width, $new_height ); // copy and resize old image into new image imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height ); // display thumbnail header("Content-type:image/jpeg"); imagejpeg( $tmp_img,$imageUrl,80); };,php,Php,调用代码的行是 <td > <?php echo createThumb("CoffeeReg.jpg",75)?> </td> 我没有发现任何正常的语法错误,代码甚至在通过语法检查器运行时恢复得很干净,这让我相信要么是我的逻辑有问题,我不正确地使用了内置函数,或者我在尝试显示新缩略图的过程中弄乱了一些东西,只是缺少经验,我看不出哪里出了问题。我需要知道我做错了什么,这样我才能从这个错误中吸取教训。好吧,经过几个小时的调试,Majid在试图帮助我理解我的

调用代码的行是

<td >
<?php echo createThumb("CoffeeReg.jpg",75)?>
</td>


我没有发现任何正常的语法错误,代码甚至在通过语法检查器运行时恢复得很干净,这让我相信要么是我的逻辑有问题,我不正确地使用了内置函数,或者我在尝试显示新缩略图的过程中弄乱了一些东西,只是缺少经验,我看不出哪里出了问题。我需要知道我做错了什么,这样我才能从这个错误中吸取教训。

好吧,经过几个小时的调试,Majid在试图帮助我理解我的代码哪里出了问题时提供的代码,我发现要使他使用的代码起作用,唯一需要做的就是在代码末尾添加这两行代码辅助php文件,如图所示

<?php
$imageUrl  = $_GET['file_name'];
$thumbWidth = $_GET['thumb_width']; 

// load image and get image size
$mainImage = imagecreatefromjpeg( $imageUrl );
$mainwidth = imagesx( $mainImage );
$mainheight = imagesy( $mainImage );

// calculate thumbnail size
$thumbWidth = intval($mainwidth/4);
$thumbHeight = intval($mainheight/4);;

// create a new temporary image
$tmp_img = imagecreatetruecolor( $thumbWidth, $thumbHeight );

// copy and resize old image into new image 
imagecopyresampled( $tmp_img, $mainImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $mainwidth, $mainheight );

// display thumbnail
header("Content-type:image/jpeg");

//Also had to remove the second and third argument from this function
imagejpeg( $tmp_img);



 //these following two lines here is what was missing
 imagedestroy(tmp_img);
 imagedestroy(mainImage);
 ?>


所以我想我们必须清除缩略图代码中的图像变量,否则它在运行时会破坏它。不确定为什么还需要从imagejpeg函数中删除这两个参数,但我的教科书中提到了这一点,所以我尝试了一下,现在通过这些更改,这段代码可以正常工作。感谢Majid对我的帮助,如果你的帖子还在这里,你将获得满分,并获得最满意的答案。

好的,在几个小时的调试后,Majid提供了代码,试图帮助我理解我的代码哪里出了问题,我发现唯一需要做的就是让他使用的代码正确工作是将这两行代码添加到第二个php文件的末尾,如下所示

<?php
$imageUrl  = $_GET['file_name'];
$thumbWidth = $_GET['thumb_width']; 

// load image and get image size
$mainImage = imagecreatefromjpeg( $imageUrl );
$mainwidth = imagesx( $mainImage );
$mainheight = imagesy( $mainImage );

// calculate thumbnail size
$thumbWidth = intval($mainwidth/4);
$thumbHeight = intval($mainheight/4);;

// create a new temporary image
$tmp_img = imagecreatetruecolor( $thumbWidth, $thumbHeight );

// copy and resize old image into new image 
imagecopyresampled( $tmp_img, $mainImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $mainwidth, $mainheight );

// display thumbnail
header("Content-type:image/jpeg");

//Also had to remove the second and third argument from this function
imagejpeg( $tmp_img);



 //these following two lines here is what was missing
 imagedestroy(tmp_img);
 imagedestroy(mainImage);
 ?>


所以我想我们必须清除缩略图代码中的图像变量,否则它在运行时会破坏它。不确定为什么还需要从imagejpeg函数中删除这两个参数,但我的教科书中提到了这一点,所以我尝试了一下,现在通过这些更改,这段代码可以正常工作。感谢Majid提供的所有帮助,如果您的帖子还在这里,您将获得满分的支持票和最佳答案。

我不相信您的
header
函数会起作用,因为“请记住,在发送任何实际输出之前必须调用header(),无论是通过普通HTML标记、文件中的空行还是PHP。”。作为
标记的第二个代码块是实际输出。另外,您的
createThumb
没有返回任何要回显的内容。的第二个参数是“路径或开放流资源…如果未设置或为NULL,原始图像流将直接输出。”我不相信您的
header
函数会工作,因为“记住,在发送任何实际输出之前,必须调用header(),无论是通过普通HTML标记、文件中的空行还是从PHP。”。作为
标记的第二个代码块是实际输出。另外,您的
createThumb
没有返回任何要回显的内容。的第二个参数是“路径或开放流资源…如果未设置或为NULL,将直接输出原始图像流。”