Archive

Archive for the ‘PHP’ Category

ฟังก์ชั่น หานามสกุลของไฟล์ ด้วยภาษา php

กันยายน 8th, 2009

function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split(”[/\\.]“, $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}

PHP

แสดง path เต็มของไฟล์บน web server

สิงหาคม 20th, 2009

เป็นโค้ดสั้นๆ ที่เพิ่งจะค้นพบ

<?php
$fullpath = getcwd();
echo
“Full path is $fullpath”;
?>

PHP

php แสดงรายการไฟล์ใน directory

มีนาคม 2nd, 2009
<html>
<head>
<title>PHP opendir()</title>
</head>
<body>
<?
$objOpen = opendir(”./”);
while (($file = readdir($objOpen)) !== false)
{
echo “filename: ” . $file . “<br />”;
}
?>
</body>
</html>

PHP

php resize รูปภาพ

มกราคม 14th, 2009

ส่วนของการเรียกใช้งาน

<? 
// 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 
?>

 

ส่วนของคลาส(resize.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"], 0000$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"], 0000$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

โค้ดเปลี่ยนขนาดรูปภาพ ที่ได้ upload

ธันวาคม 5th, 2008

Part I: The HTML Form

<form action="upload.php" method="post" enctype="multipart/form-data" >
<input type="file" name="uploadfile"/>
<input type="submit"/>
</form>

Part II: Getting at the file, resizing the image, and saving to the server. (upload.php)

<?php

// This is the temporary file created by PHP
$uploadedfile = $_FILES['uploadfile']['tmp_name'];

// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);

// Capture the original size of the uploaded image
list($width,$height)=getimagesize($uploadedfile);

// For our purposes, I have resized the image to be
// 600 pixels wide, and maintain the original aspect
// ratio. This prevents the image from being “stretched”
// or “squashed”. If you prefer some max width other than
// 600, simply change the $newwidth variable
$newwidth=600;
$newheight=($height/$width)*600;
$tmp=imagecreatetruecolor($newwidth,$newheight);

// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

// now write the resized image to disk. I have assumed that you want the
// resized, uploaded image file to reside in the ./images subdirectory.
$filename = “images/”. $_FILES['uploadfile']['name'];
imagejpeg($tmp,$filename,100);

imagedestroy($src);
imagedestroy($tmp);
// NOTE: PHP will clean up the temp file it created when the request
// has completed.
?>

PHP