PHP-ImageCopyResampled问题 [英] PHP - Problem with ImageCopyResampled

查看:62
本文介绍了PHP-ImageCopyResampled问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用ImageCopyResampled函数.我只是按照 PHP手册中编写的代码进行操作.运行代码时似乎没有错误.问题是我的代码基本上只是复制原始图像,并且没有遵循在函数中传递的参数中定义的尺寸.下面是我的代码:

It's my first time using the function ImageCopyResampled. I just followed the code written in the PHP manual. There seemed to be no errors when I ran the code. The problem is my code just basically copies the original image and did not follow the dimensions as it was defined in the parameters passed in the function. Below is my code:

    public static function uploadFile($filename, $x_dimension, $y_dimension, $width, $height){
        $file =   DOCROOT . "uploads/temp/".$filename;
        $trgt_file = DOCROOT . "uploads/images/thumbs/".$filename; 

        if(is_file($file) AND file_exists($file)):
                $trgt_w = 198;
                $trgt_h = 130;
                if(copy($file, $trgt_file)):
                        $src_img = imageCreateFromJpeg($file);
                        $trgt_img = imageCreateTrueColor($trgt_w, $trgt_h);
                        imageCopyResampled($trgt_img, $src_img, 0, 0, $x_dimension, $y_dimension, $trgt_w, $trgt_h, $width ,$height);
                        unlink($file);  
                endif;
        endif;
}

此功能仅复制源文件,并且没有裁剪.我想念什么?

This function just copy the source file and no cropping happened. What did I miss?

顺便说一句,我正在使用kohana3.谢谢.

BTW, Im using kohana 3. Thanks.

推荐答案

您没有将$trgt_img保存到文件,因此在脚本结束时裁剪的图像会丢失.

You are not saving $trgt_img to a file, so the cropped image gets lost when the script ends.

您需要使用 imageJPEG() (或您要编写的任何格式)写出数据).

You need to write out the data using imageJPEG() (or whatever format you want to write to).

imageCopyResampled($trgt_img, $src_img, 0, 0, 
                   $x_dimension, $y_dimension, $trgt_w, $trgt_h, 
                   $width ,$height);

imagejpeg($trgt_img, $filename, 90);  // 90 is for quality - 75 is the default

这篇关于PHP-ImageCopyResampled问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆