php網站怎麼修改圖片

發布: 2023-02-27 19:02:01
原創
3735 人瀏覽過

php網站怎麼修改圖片

主要還是用到php中的GD庫中的函數

upload_image.php,主要是一個上傳控件,用來選擇圖片

r​​rreee

然後是上傳和處理圖片的邏輯check_image.php

<html>
    <head>
        <title></title>
        <style type="text/css"></style>
    </head>
    <body>
        <form action="check_image.php" method="post" enctype="multipart/form-data">
            <table>
                <tr>
                    <td>Your username</td>
                    <td><input type="text" name="username" /></td>
                </tr>
                <tr>
                    <td>Upload image*</td>
                    <td><input type="file" name="uploadfile"/></td>
                </tr>
                <tr>
                    <td colspan="2">
                        <small><em> * Acceptable image formats include: GIF, JPG/JPEG and PNG.</em></small>
                    </td>
                </tr>
                <tr>
                    <td>Image Caption</td>
                    <td><input type="text" name="caption"/></td>
                </tr>
                <tr>
                    <td colspan="2" style="text-align:center;">
                        <input type="submit" name="submit" value="Upload" />
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>
登入後複製

最後是一個預覽效果的頁面image_effect.php

<?php 
//修改图片效果
$db = mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;Ctrip07185419&#39;) or die(&#39;can not connect to database&#39;);
mysql_select_db(&#39;moviesite&#39;,$db) or die(mysql_error($db));
//上传文件的路径
$dir = &#39;D:\Serious\phpdev\test\images&#39;;

//upload_image.php页面传递过来的参数,如果是上传图片
if($_POST[&#39;submit&#39;] == &#39;Upload&#39;)
{
    if($_FILES[&#39;uploadfile&#39;][&#39;error&#39;] != UPLOAD_ERR_OK)
    {
        switch($_FILES[&#39;uploadfiel&#39;][&#39;error&#39;])
        {
            case UPLOAD_ERR_INI_SIZE:
                die(&#39;The uploaded file exceeds the upload_max_filesize directive&#39;);
            break;
            case UPLOAD_ERR_FORM_SIZE:
                die(&#39;The upload file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form&#39;);
            break;
            case UPLOAD_ERR_PARTIAL:
                die(&#39;The uploaded file was only partially uploaded&#39;);
            break;
            case UPLOAD_ERR_NO_FILE:
                die(&#39;No file was uploaded&#39;);
            break;
            case UPLOAD_ERR_NO_TMP_DIR:
                die(&#39;The server is missing a temporary folder&#39;);
            break;    
            case UPLOAD_ERR_CANT_WRITE:
                die(&#39;The server fail to write the uploaded file to the disk&#39;);
            break;        
            case UPLOAD_ERR_EXTENSION:
                die(&#39;The upload stopped by extension&#39;);
            break;                
        }
    }
    $image_caption = $_POST[&#39;caption&#39;];
    $image_username = $_POST[&#39;username&#39;];
    $image_date = date(&#39;Y-m-d&#39;);
    list($width,$height,$type,$attr) = getimagesize($_FILES[&#39;uploadfile&#39;][&#39;tmp_name&#39;]);
    $error = &#39;The file you upload is not a supported filetype&#39;;
    switch($type)
    {
        case IMAGETYPE_GIF:
            $image = imagecreatefromgif($_FILES[&#39;uploadfile&#39;][&#39;tmp_name&#39;]) or die($error);
        break;
        case IMAGETYPE_JPEG:
            $image = imagecreatefromjpeg($_FILES[&#39;uploadfile&#39;][&#39;tmp_name&#39;]) or die($error);
        break;
        case IMAGETYPE_PNG:
            $image = imagecreatefrompng($_FILES[&#39;uploadfile&#39;][&#39;tmp_name&#39;]) or die($error);
        break;
        default:
        break;
    }
    $query = &#39;insert into images(image_caption,image_username,image_date) values("&#39;.$image_caption.&#39;" , "&#39;.$image_username.&#39;","&#39;.$image_date.&#39;")&#39;;
    $result = mysql_query($query,$db) or die(mysql_error($db));
    $last_id = mysql_insert_id();
    
    // $imagename = $last_id.&#39;.jpg&#39;;
    // imagejpeg($image,$dir.&#39;/&#39;.$imagename);
    // imagedestroy($image);
    
    $image_id = $last_id;
    imagejpeg($image , $dir.&#39;/&#39;.$image_id.&#39;.jpg&#39;);
    imagedestroy($image);
}
else  //如果图片已经上传,则从数据库中取图片名字
{
    $query = &#39;select image_id,image_caption,image_username,image_date from images where image_id=&#39;.$_POST[&#39;id&#39;];
    $result = mysql_query($query,$db) or die(mysql_error($db));
    extract(mysql_fetch_assoc($result));
    list($width,$height,$type,$attr) = getimagesize($dir.&#39;/&#39;.$image_id.&#39;.jpg&#39;);
}

//如果是保存图片
if($_POST[&#39;submit&#39;] == &#39;Save&#39;)
{
    if(isset($_POST[&#39;id&#39;]) && ctype_digit($_POST[&#39;id&#39;]) && file_exists($dir.&#39;/&#39;.$_POST[&#39;id&#39;].&#39;.jpg&#39;))
    {
        $image = imagecreatefromjpeg($dir.&#39;/&#39;.$_POST[&#39;id&#39;].&#39;.jpg&#39;);
    }
    else
    {
        die(&#39;invalid image specified&#39;);
    }
    $effect = (isset($_POST[&#39;effect&#39;])) ? $_POST[&#39;effect&#39;] : -1;
    switch($effect)
    {
        case IMG_FILTER_NEGATE:
            imagefilter($image , IMG_FILTER_NEGATE);     //将图像中所有颜色反转
        break;
        case IMG_FILTER_GRAYSCALE:
            imagefilter($image , IMG_FILTER_GRAYSCALE);  //将图像转换为灰度的
        break;
        case IMG_FILTER_EMBOSS:
            imagefilter($image , IMG_FILTER_EMBOSS);     //使图像浮雕化
        break;
        case IMG_FILTER_GAUSSIAN_BLUR:
            imagefilter($image , IMG_FILTER_GAUSSIAN_BLUR); //用高斯算法模糊图像
        break;    
    }
    imagejpeg($image , $dir.&#39;/&#39;.$_POST[&#39;id&#39;].&#39;.jpg&#39; , 100);
    ?>
    <html>
        <head>
            <title>Here is your pic!</title>
        </head>
        <body>
            <h1>Your image has been saved!</h1>
            <img src="images/<?php echo $_POST[&#39;id&#39;];?>.jpg" alt="" />
        </body>
    </html>
<?php 
}
else
{
?>
    <html>
        <head>
            <title>Here is your pic!</title>
        </head>
        <body>
            <h1>So how does it feel to be famous?</h1>
            <p>Here is the picture you just uploaded to your servers:</p>
            <!--<img src="images/<?php echo $imagename;?>" alt="" style="float:left;" />-->
        </body>
    </html>
    <?php
        if($_POST[&#39;submit&#39;] == &#39;Upload&#39;)
        {
            $imagename = &#39;images/&#39;.$image_id.&#39;.jpg&#39;;
        }
        else
        {
            $imagename = &#39;image_effect.php?id=&#39;.$image_id.&#39;&e=&#39;.$_POST[&#39;effect&#39;];
        }
    ?>
    <img src="<?php echo $imagename;?>" style="float:left;" alt="" />
    <table>
        <tr>
            <td>Image save as:</td>
            <td><?php $image_id?></td>
        </tr>
        <tr>
            <td>Height:</td>
            <td><?php echo $height;?></td>
        </tr>
        <tr>
            <td>Widht:</td>
            <td><?php echo $width;?></td>
        </tr>
        <tr>
            <td>Upload date:</td>
            <td><?php echo $image_date;?></td>
        </tr>
    </table>
    <p>You may apply a special effect to your image from the list of option below.
    Note:saving an image with any of the filters applied <em>can be undone</em>
    </p>
    <form action="<?php echo $_SERVER[&#39;PHP_SELF&#39;];?>" method="post">
        <div>
            <input type="hidden" name="id" value="<?php echo $image_id;?>"/>
            <select name="effect" id="">
                <option value="-1">None</option>
                <?php 
                    echo &#39;<option value="&#39;.IMG_FILTER_GRAYSCALE.&#39;" &#39;;
                    if(isset($_POST[&#39;effect&#39;]) && $_POST[&#39;effect&#39;] == IMG_FILTER_GRAYSCALE)
                    {
                        echo &#39;selected="selected"&#39;;
                    }
                    echo &#39; >Black and white</option>&#39;;
                    
                    echo &#39;<option value="&#39;.IMG_FILTER_GAUSSIAN_BLUR.&#39;"&#39;;
                    if(isset($_POST[&#39;effect&#39;]) && $_POST[&#39;effect&#39;] == IMG_FILTER_GAUSSIAN_BLUR)
                    {
                        echo &#39; selected="selected"&#39;;
                    }
                    echo &#39;>Blur</option>&#39;;
                    
                    echo &#39;<option value="&#39;.IMG_FILTER_EMBOSS.&#39;"&#39;;
                    if(isset($_POST[&#39;effect&#39;]) && $_POST[&#39;effect&#39;] == IMG_FILTER_EMBOSS)
                    {
                        echo &#39;selected="selected"&#39;;
                    }
                    echo &#39;>Emboss</option>&#39;;
                    
                    echo &#39;<option value="&#39;.IMG_FILTER_NEGATE.&#39;"&#39;;
                    if(isset($_POST[&#39;effect&#39;]) && $_POST[&#39;effect&#39;] == IMG_FILTER_NEGATE)
                    {
                        echo &#39;selected="selected"&#39;;
                    }
                    echo &#39;>Negative</option>&#39;;
                ?>
            </select><br />
            <input type="submit" value="Preview" name="submit" /><br />
            <input type="submit" value="Save" name="submit" />
            
        </div>
    </form>
<?php 
}
?>
登入後複製

當使用imagefilter方法處理圖片之後會把圖片輸出到頁面,這裡要注意imagejpeg方法的第二個參數是空字串,這樣它就不會寫入到硬碟了,如果第二個參數設定了會覆蓋原有的圖片,這樣可以讓使用者在儲存圖片之前隨意的預覽效果,如下:

<?php 
$dir = &#39;D:\Serious\phpdev\test\images&#39;;

if(isset($_GET[&#39;id&#39;]) && ctype_digit($_GET[&#39;id&#39;]) && file_exists($dir.&#39;/&#39;.$_GET[&#39;id&#39;].&#39;.jpg&#39;))
{
    $image = imagecreatefromjpeg($dir.&#39;/&#39;.$_GET[&#39;id&#39;].&#39;.jpg&#39;);
}
else
{
    die(&#39;invalid image specified&#39;);
}

$effect = (isset($_GET[&#39;e&#39;])) ? $_GET[&#39;e&#39;] : -1;

switch($effect)
{
    case IMG_FILTER_NEGATE:
        imagefilter($image , IMG_FILTER_NEGATE);
    break;
    case IMG_FILTER_GRAYSCALE:
        imagefilter($image , IMG_FILTER_GRAYSCALE);
    break;    
    case IMG_FILTER_EMBOSS:
        imagefilter($image , IMG_FILTER_EMBOSS);
    break;    
    case IMG_FILTER_GAUSSIAN_BLUR:
        imagefilter($image , IMG_FILTER_GAUSSIAN_BLUR);
    break;    
}
header(&#39;Content-Type:image/jpeg&#39;);
imagejpeg($image , &#39;&#39; , 100);

?>
登入後複製

在check_image.php中有呼叫到類似的方法,但這裡指定了第二個參數,就是用來保存圖片的:

header(&#39;Content-Type:image/jpeg&#39;);
imagejpeg($image , &#39;&#39; , 100);
登入後複製

php中處理圖片的方法:

IMG_FILTER_NEGATE:將影像中所有色彩反轉。

IMG_FILTER_GRAYSCALE:將影像轉換為灰階的。

IMG_FILTER_BRIGHTNESS:改變影像的亮度。用 arg1 設定亮度等級。

IMG_FILTER_CONTRAST:改變影像的對比。用 arg1 設定對比等級。

IMG_FILTER_COLORIZE:與 IMG_FILTER_GRAYSCALE 類似,不過可以指定顏色。用 arg1,arg2 和 arg3 分別指定 red,blue 和 green。每種顏色範圍是 0 到 255。

IMG_FILTER_EDGEDETECT:用邊緣偵測來突顯影像的邊緣。

IMG_FILTER_EMBOSS:使影像浮雕化。

IMG_FILTER_GAUSSIAN_BLUR:用高斯演算法模糊影像。

IMG_FILTER_SELECTIVE_BLUR:模糊影像。

IMG_FILTER_MEAN_REMOVAL:用平均移除法來達到輪廓效果。

IMG_FILTER_SMOOTH:使影像更柔滑。用 arg1 設定柔滑等級。

以上是php網站怎麼修改圖片的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!