图像在上传时调整大小

时间:2015-06-29 07:21:47

标签: php

我正在尝试在PHP中调整上传的图像。

到目前为止,我已经实现了以下代码:

    $location=$firstname.rand().".jpg";// URL, file will be saved in images folder
$filename="images/".$location;
    $fname="image_photo";//name of the input field for file upload
    //$filename="images/".$query+1.".jpg";// URL, file will be saved in images folder
            if(is_uploaded_file($_FILES["$fname"]['tmp_name']))
            {
                if(copy($_FILES["$fname"]['tmp_name'],$filename))
                {

                    $thumb_path = "images/thumbs/".$location;
                    $src_img = imagecreatefromjpeg("$filename"); 

                    $origw=imagesx($src_img); 

                    $origh=imagesy($src_img); 


                    $dst_img = imagecreatetruecolor(125,150); 

                        $new_w = 125; 
                        $new_h=round(($origh/$origw)*$new_w);

                    if($new_h<150)
                    {
                        $new_h=150;
                        $new_w=round(($origw/$origh)*$new_h);
                        $srcx=round(($new_w-125)/2);
                        imagecopyresampled($dst_img,$src_img,0,0,$srcx,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img)); 
                        $x= imagejpeg($dst_img, "$thumb_path",150);     
                    }

                    else
                    {
                        $new_w=125;
                        $new_h=round(($origh/$origw)*$new_w);
                        $srcy=round(($new_h-150)/2);
                        imagecopyresampled($dst_img,$src_img,0,0,0,$srcy,$new_w,$new_h,imagesx($src_img),imagesy($src_img)); 
                        $x=imagejpeg($dst_img, "$thumb_path",150);
                    }

**mysql update query***

                }
            }
            else
            {
                echo "please upload a photo";
            }

它在localhost上完美运行。但是当我上传到服务器时,它无法正常工作。

欢迎任何帮助

0 个答案:

没有答案