ขอโค้ด resize รูปหน่อย

ขอ code php หน่อยนะครับ
10 พ.ย. 2552 4 2,306

#1

if($file)//รับค่าจาก textbox ชื่อ file{
$fileType = explode('.', $file_name);
$fileName = date('YmdHis');
$fileName = $fileName.".".$fileType[1];
$uploadFile = $path."".$fileName;
$images = "$file";
$width=150; //*** Fix Width & Heigh (Autu caculate) ***//
$size=GetimageSize($images);
$height=round($width*$size[1]/$size[0]);
if($size[2] == 1) {
$images_orig = ImageCreateFromGIF($images); //รูปภาพประเภท gif
} else if($size[2] == 2) {
$images_orig = ImageCreateFromJPEG($images);  //รูปภาพประเภท jpeg
}
$photoX = ImagesX($images_orig);
$photoY = ImagesY($images_orig);
$images_fin = ImageCreateTrueColor($width, $height);
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
ImageJPEG($images_fin,$fileName);
ImageDestroy($images_orig);
ImageDestroy($images_fin);
}

//ลองดูครับ
#2

<?php
/*
|---------------------------------------------------------------------------------------
|create by Surachet Khumboonmee
|---------------------------------------------------------------------------------------
if fix = 1
$vals = array(
        'source' => './uploads/exsample/images/',
        'targetPath' => './uploads/exsample/thumb/',
        'w' => 150,
        'h' => 150,
        'fix' => 1
);
resize_crop_image($vals);

if fix = ''
$vals = array(
        'source' => './uploads/exsample/images/',
        'targetPath' => './uploads/exsample/thumb/',
        'w' => 150,
        'h' => 150,
        'set_y' => 30
);
resize_crop_image($vals);
*/

   
function resize_crop_image($data='')
{
    $defaults = array('source'=>'', 'targetPath'=>'', 'w'=>150, 'h'=>150, 'set_y'=>0, 'fix'=>'');       

    foreach ($defaults as $key => $val)
    {
        if ( ! is_array($data))
        {
            if ( ! isset($$key) OR $$key == '')
            {
                $$key = $val;
            }
        }
        else
        {           
            $$key = ( ! isset($data[$key])) ? $val : $data[$key];
        }
    }
   
    $create_x = $w;
    $create_y = $h;
    if($w<=0) $w=100;
    if(substr($targetPath, -1)!="/") $targetPath.="/";
   
    $exFile=explode("/", $source);
    $fileSource=$exFile[count($exFile)-1];

    $imgFileThumb = $targetPath."thumb_".substr($fileSource,0,-3).substr($fileSource,-3);
    $pathAndFile->path = $targetPath;
    $pathAndFile->filename = "thumb_".substr($fileSource,0,-3).substr($fileSource,-3);

    #  Thumbnail
    if(file_exists($source) && is_file($source))
    {
             
            if(strtolower(substr($source,-3))==="jpg")  $src_img=imagecreatefromjpeg($source);
            else if(strtolower(substr($source,-3))==="gif")  $src_img=imagecreatefromgif($source);
            else if(strtolower(substr($source,-3))==="png")  $src_img=imagecreatefrompng($source);

            $getsize=@getimagesize($source);
            $old_w=$getsize[0];
            $old_h=$getsize[1];

            #  $fix = 1 ?
            if($fix==1)
            {
                $new_h=$h;
                $new_w=$w;
            }
            #  $fix  resize
            else
            {
                if (($old_w<$w) and ($old_h<$h))
                {
                    $new_h=$old_h;
                    $new_w=$old_w;
                }
                else
                {
                    $how=$old_w/$w;
                    $new_w=floor($old_w/$how);
                    $new_h=floor($old_h/$how);
                }
            }

            $dst_img=imagecreatetruecolor($new_w,$new_h);
            imageCopyResampled($dst_img,$src_img,0,0,0,0,$new_w,$new_h,$old_w,$old_h);
            $x = imagesx($dst_img);
            $set_x = floor(($x-$create_x)/2);

            $dst_img=imagecreatetruecolor($create_x, $create_y);
           
            imageCopyResampled($dst_img,$src_img,0,0,$set_x,$set_y,$new_w,$new_h,$old_w,$old_h);
             


            if(strtolower(substr($source,-3))==="jpg")  imagejpeg($dst_img , $imgFileThumb , 90);
            else if(strtolower(substr($source,-3))==="gif")  imagegif($dst_img , $imgFileThumb);
            else if(strtolower(substr($source,-3))==="png")  imagepng($dst_img , $imgFileThumb);
           
            imagedestroy($src_img);
            imagedestroy($dst_img);

            return $pathAndFile;
    }
        else return false;
}
?>
#3

เห็นมะครับ คำถามเดียว แต่มีคำตอบได้หลายรูปแบบเชียวครับ
#4
ความคิดเห็น
ไฟล์อัปโหลด ชนิด jpg, jpeg ขนาดไฟล์ไม่เกิน 1024
^