donglie7778 2017-11-07 05:04
浏览 76

我的缩略图功能无法正常工作

When this code runs the browser screen turns black with a grey and white checkered square in the middle when it is suppose to display a thumbnail within a table cell on a form.

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

And the line that calls the code is

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

I haven't found any of the normal syntax errors, and the code even comes back clean when ran through a syntax checker so that leads me to believe either its a problem with my logic, I'm using the built in functions incorrectly or I have messed up something in the way I am trying to display the new thumbnail only with my lack of exp I don't see where I went wrong. I need to know what I did wrong so I can learn from this mistake.

  • 写回答

1条回答 默认 最新

  • download12749 2017-11-07 16:48
    关注

    Okay so after several hours debugging the code that Majid had provided when trying to help me understand where I went wrong with my code I found that the only thing that was needed to make the code he was using work was to add these two lines of code to the end of the secondary php file as shown here...

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

    So I guess that one must clear out the image variables of the thumbnail code or it will break it when it runs. Not sure why removing the two arguments from the imagejpeg function was also needed but it was mentioned in my textbook so i tried it, now with these changes this code works. Thank you Majid for all your help and If your post was still here you would be getting full credit with an up vote and best answer.

    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探