ايران ويج

نسخه‌ی کامل: تابع resize کردن عکس
شما در حال مشاهده‌ی نسخه‌ی متنی این صفحه می‌باشید. مشاهده‌ی نسخه‌ی کامل با قالب بندی مناسب.
سلام
من یه تابع resize کردن عکس با php نوشتم
اما کار نمیکنه دوستان لطفا یه نگاه بندازید بهش
کد بررسی میکنه اگه عکس از سایز مورد نظر بیشتر بود تابع رو فرا میخونه
ورودی های تابع :

- آدرس عکس
- newWidth
- newHeight
- کیفیت تصویر
- اسم عکس بعد از تغییرات

کد php:
<?php
$filename
="test.jpg";

list(
$width,$height)=getimagesize($filename);
echo 
"width= ".$width."<br />";
echo 
"height= ".$height;

//Image cover size
if($width<=220 && $height<=150){
    echo 
"سایز مناست است";
}
else{
//Resize
    
$QualityPic=100;
    
Resize($filename,60,40,$QualityPic,"new.jpg");
}

function 
Resize($filename,$newWidth,$newHeight,$compression,$SaveName){
    
$image imagecreatefromjpeg($filename);
    if(
$image){
        
$new_image imagecreatetruecolor($newWidth$newHeight);

        
$gWidth=imagesx($image);
        
$gHeight=imagesy($image);

        
imagecopyresampled($new_image,$image0000$newWidth$newHeight$gWidth$gHeight);
        
imagejpeg($image,$SaveName,$compression);
        
imagedestroy($image);
    }
    else{
        echo 
"error";
    }
}
?>
نکته: من با xp و wamp کار میکنم
وقتی کد php میخوام اجرا کنم به من ارور نمیده ها یه پیام dont send میده برای Apache Http Server
یعنی apache به مشکل بر میخوره
دوستان کد بالا رو خودتون در سیستم اجرا کنید و بگید apache شما هم به این مشکل میخوره آیا !

قسمت زیر رو :
کد:
imagejpeg($image,$SaveName,$compression);
به این تبدیل کردم :
کد:
imagejpeg($new_image,$SaveName,$compression);
اما بازم مشکل بالا رو دارم
ببین این کد کارت را حل میکنه؟
کد:
function thumbnail($img,$size,$imagebasename,$filetype)
{
    $image = $img ;
    $imagebasename="thumb".$imagebasename;
    $width  = $size[0];
    $height = $size[1];

    $image_dir = "images/uploaded/product";      
    $newwidth  = 130 ;
    $newheight = 90 ;
    
    
    if($filetype == 'image/pjpeg' ) $src = imagecreatefromjpeg($image);
    else if($filetype == 'image/gif'   ) $src = imagecreatefromgif($image);
    else if($filetype == 'image/jpeg'  ) $src = imagecreatefromjpeg($image);
        
    $im = imagecreatetruecolor($newwidth,$newheight);
          imagecopyresampled($im,$src,0,0,0,0,$newwidth,$new  height,$width,$height);

          imagejpeg($im, "../$image_dir/thumb/$imagebasename");
          imagedestroy($im);
}

این یکی را هم امتحان کن
کد:
<?php
##############################################
# Shiege Iseng Resize Class
# 11 March 2003
# shiegege_at_yahoo.com
# View Demo :
#   http://shiege.com/scripts/thumbnail/
/*############################################
Sample :
$thumb=new thumbnail("./shiegege.jpg");            // generate image_file, set filename to resize
$thumb->size_width(100);                // set width for thumbnail, or
$thumb->size_height(300);                // set height for thumbnail, or
$thumb->size_auto(200);                    // set the biggest width or height for thumbnail
$thumb->jpeg_quality(75);                // [OPTIONAL] set quality for jpeg only (0 - 100) (worst - best), default = 75
$thumb->show();                        // show your thumbnail
$thumb->save("./huhu.jpg");                // save your thumbnail to file
----------------------------------------------
Note :
- GD must Enabled
- Autodetect file extension (.jpg/jpeg, .png, .gif, .wbmp)
  but some server can't generate .gif / .wbmp file types
- If your GD not support 'ImageCreateTrueColor' function,
  change one line from 'ImageCreateTrueColor' to 'ImageCreate'
  (the position in 'show' and 'save' function)
*/############################################


class thumbnail
{
    var $img;

    function thumbnail($imgfile)
    {
        //detect image format
        $this->img["format"]=ereg_replace(".*\.(.*)$","\\1",$imgfile);
        $this->img["format"]=strtoupper($this->img["format"]);
        if ($this->img["format"]=="JPG" || $this->img["format"]=="JPEG") {
            //JPEG
            $this->img["format"]="JPEG";
            $this->img["src"] = ImageCreateFromJPEG ($imgfile);
        } elseif ($this->img["format"]=="PNG") {
            //PNG
            $this->img["format"]="PNG";
            $this->img["src"] = ImageCreateFromPNG ($imgfile);
        } elseif ($this->img["format"]=="GIF") {
            //GIF
            $this->img["format"]="GIF";
            $this->img["src"] = ImageCreateFromGIF ($imgfile);
        } elseif ($this->img["format"]=="WBMP") {
            //WBMP
            $this->img["format"]="WBMP";
            $this->img["src"] = ImageCreateFromWBMP ($imgfile);
        } else {
            //DEFAULT
            echo "Not Supported File";
            exit();
        }
        @$this->img["lebar"] = imagesx($this->img["src"]);
        @$this->img["tinggi"] = imagesy($this->img["src"]);
        //default quality jpeg
        $this->img["quality"]=75;
    }

    function size_height($size=100)
    {
        //height
        $this->img["tinggi_thumb"]=$size;
        @$this->img["lebar_thumb"] = ($this->img["tinggi_thumb"]/$this->img["tinggi"])*$this->img["lebar"];
    }

    function size_width($size=100)
    {
        //width
        $this->img["lebar_thumb"]=$size;
        @$this->img["tinggi_thumb"] = ($this->img["lebar_thumb"]/$this->img["lebar"])*$this->img["tinggi"];
    }

    function size_auto($size=100)
    {
        //size
        if ($this->img["lebar"]>=$this->img["tinggi"]) {
            $this->img["lebar_thumb"]=$size;
            @$this->img["tinggi_thumb"] = ($this->img["lebar_thumb"]/$this->img["lebar"])*$this->img["tinggi"];
        } else {
            $this->img["tinggi_thumb"]=$size;
            @$this->img["lebar_thumb"] = ($this->img["tinggi_thumb"]/$this->img["tinggi"])*$this->img["lebar"];
         }
    }

    function jpeg_quality($quality=75)
    {
        //jpeg quality
        $this->img["quality"]=$quality;
    }

    function show()
    {
        //show thumb
        @Header("Content-Type: image/".$this->img["format"]);

        /* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function*/
        $this->img["des"] = ImageCreateTrueColor($this->img["lebar_thumb"],$this->img["tinggi_thumb"]);
            @imagecopyresized ($this->img["des"], $this->img["src"], 0, 0, 0, 0, $this->img["lebar_thumb"], $this->img["tinggi_thumb"], $this->img["lebar"], $this->img["tinggi"]);

        if ($this->img["format"]=="JPG" || $this->img["format"]=="JPEG") {
            //JPEG
            imageJPEG($this->img["des"],"",$this->img["quality"]);
        } elseif ($this->img["format"]=="PNG") {
            //PNG
            imagePNG($this->img["des"]);
        } elseif ($this->img["format"]=="GIF") {
            //GIF
            imageGIF($this->img["des"]);
        } elseif ($this->img["format"]=="WBMP") {
            //WBMP
            imageWBMP($this->img["des"]);
        }
    }

    function save($save="")
    {
        //save thumb
        if (empty($save)) $save=strtolower("./thumb.".$this->img["format"]);
        /* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function*/
        $this->img["des"] = ImageCreateTrueColor($this->img["lebar_thumb"],$this->img["tinggi_thumb"]);
            @imagecopyresized ($this->img["des"], $this->img["src"], 0, 0, 0, 0, $this->img["lebar_thumb"], $this->img["tinggi_thumb"], $this->img["lebar"], $this->img["tinggi"]);

        if ($this->img["format"]=="JPG" || $this->img["format"]=="JPEG") {
            //JPEG
            imageJPEG($this->img["des"],"$save",$this->img["quality"]);
        } elseif ($this->img["format"]=="PNG") {
            //PNG
            imagePNG($this->img["des"],"$save");
        } elseif ($this->img["format"]=="GIF") {
            //GIF
            imageGIF($this->img["des"],"$save");
        } elseif ($this->img["format"]=="WBMP") {
            //WBMP
            imageWBMP($this->img["des"],"$save");
        }
    }
}
?>
و روش استفاده
کد:
<?php
// see online : http://kentung.f2o.org/scripts/thumbnail/sample.php
include_once("resize.php");
$thumb=new thumbnail("/www/shiegege.jpg"); // prepare to generate "shiegege.jpg" in directory "/www"
$thumb->size_width(100);           // set width for thumbnail with 100 pixels
$thumb->show();                   // show my thumbnail
$thumb->save("/www/thumb/huhu.jpg");       // save my  thumbnail to file "huhu.jpg" in directory "/www/thumb
?>

اینم هست
کد:
<?php
function resize_image($file,$new_file)
{
    $new_width=150;
    $new_height=200;
    if(!extension_loaded('gd')&&!extension_loaded('gd2'))  {
        die("GD is not installed!");
    }
    list($width,$height,$type)=getimagesize($file);
    switch($type)
    {
        case 1:$img=imagecreatefromgif($file);break;
        case 2:$img=imagecreatefromjpeg($file);break;
        case 3:$img=imagecreatefrompng($file);break;
        default:die('Unsknown file!');
    }
    $ratio=(float)$height/$width;
    $new_ratio=(float)$new_height/$new_width;
    if($new_ratio>$ratio)$new_height=round($new_width*$ratio);
    else $new_width=round($new_height/$ratio);
    $new_img=imagecreatetruecolor($new_width,$new_height);
    if(($type==1)||($type==3)){
        imagealphablending($new_img,false);
        imagesavealpha($new_img,true);
        $tmp=imagecolorallocatealpha($new_img,255,255,255,127);
        imagefilledrectangle($new_img,0,0,$new_width,$new_height,$tmp);
    }
    imagecopyresampled($new_img,$img,0,0,0,0,$new_width,$new_height,$width,$height);
    switch($type)
    {
        case 1:imagegif($new_img,$new_file);break;
        case 2:imagejpeg($new_img,$new_file);break;
        case 3:imagepng($new_img,$new_file);break;
        default:die('Failed resize image!');
    }
}