Jadual Kandungan
Your image has been saved!
So how does it feel to be famous?
Rumah php教程 php手册 PHP根据图片制作缩略图

PHP根据图片制作缩略图

Jun 06, 2016 pm 07:46 PM
php buat gambar kaedah

php中制作缩略图的方法也很简单,是用imagecopyresampled方法根据源图制作一个小一点的图片,来看代码check_image_addthumbs.php ? php // 修改图片效果 $db = mysql_connect ('localhost','root','Ctrip07185419') or die ('can not connect to database' )

php中制作缩略图的方法也很简单,是用imagecopyresampled方法根据源图制作一个小一点的图片,来看代码check_image_addthumbs.php

<span>php 
</span><span>//</span><span>修改图片效果</span>
<span>$db</span> = <span>mysql_connect</span>('localhost','root','Ctrip07185419') or <span>die</span>('can not connect to database'<span>);
</span><span>mysql_select_db</span>('moviesite',<span>$db</span>) or <span>die</span>(<span>mysql_error</span>(<span>$db</span><span>));
</span><span>//</span><span>上传文件的路径</span>
<span>$dir</span> = 'D:\Serious\phpdev\test\images'<span>;
</span><span>//</span><span>缩略图的路径</span>
<span>$thumbdir</span> = 'D:\Serious\phpdev\test\images\thumbs'<span>;
</span><span>//</span><span>设置环境变量</span>
<span>putenv</span>('GDFONTPATH='.'C:\Windows\Fonts'<span>);
</span><span>$font</span> = "arial"<span>;

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

</span><span>//</span><span>如果是保存图片</span>
<span>if</span>(<span>$_POST</span>['submit'] == 'Save'<span>)
{
    </span><span>if</span>(<span>isset</span>(<span>$_POST</span>['id']) && <span>ctype_digit</span>(<span>$_POST</span>['id']) && <span>file_exists</span>(<span>$dir</span>.'/'.<span>$_POST</span>['id'].'.jpg'<span>))
    {
        </span><span>$image</span> = imagecreatefromjpeg(<span>$dir</span>.'/'.<span>$_POST</span>['id'].'.jpg'<span>);
    }
    </span><span>else</span><span>
    {
        </span><span>die</span>('invalid image specified'<span>);
    }
    </span><span>$effect</span> = (<span>isset</span>(<span>$_POST</span>['effect'])) ? <span>$_POST</span>['effect'] : -1<span>;
    </span><span>switch</span>(<span>$effect</span><span>)
    {
        </span><span>case</span> IMG_FILTER_NEGATE:<span>
            imagefilter(</span><span>$image</span> , IMG_FILTER_NEGATE);     <span>//</span><span>将图像中所有颜色反转</span>
        <span>break</span><span>;
        </span><span>case</span> IMG_FILTER_GRAYSCALE:<span>
            imagefilter(</span><span>$image</span> , IMG_FILTER_GRAYSCALE);  <span>//</span><span>将图像转换为灰度的</span>
        <span>break</span><span>;
        </span><span>case</span> IMG_FILTER_EMBOSS:<span>
            imagefilter(</span><span>$image</span> , IMG_FILTER_EMBOSS);     <span>//</span><span>使图像浮雕化</span>
        <span>break</span><span>;
        </span><span>case</span> IMG_FILTER_GAUSSIAN_BLUR:<span>
            imagefilter(</span><span>$image</span> , IMG_FILTER_GAUSSIAN_BLUR); <span>//</span><span>用高斯算法模糊图像</span>
        <span>break</span><span>;    
    }
    
    </span><span>if</span>(<span>isset</span>(<span>$_POST</span>['emb_caption'<span>]))
    {
        imagettftext(</span><span>$image</span> , 12 , 0 , 20 , 20 , 0 , <span>$font</span> , <span>$image_caption</span><span>);
    }
    
    </span><span>$thumb_width</span> = <span>$width</span> * 0.10<span>;
    </span><span>$thumb_height</span> = <span>$height</span> * 0.10<span>;
                   
    </span><span>//</span><span>创建一个缩略图</span>
    <span>$thumb</span> = imagecreatetruecolor(<span>$thumb_width</span>, <span>$thumb_height</span><span>);
    imagecopyresampled(</span><span>$thumb</span>, <span>$image</span>, 0, 0, 0, 0, <span>$thumb_width</span>, <span>$thumb_height</span>,    <span>$width</span>, <span>$height</span><span>);
    </span><span>//</span><span>保存原图</span>
    imagejpeg(<span>$image</span>, <span>$dir</span> . '/' . <span>$_POST</span>['id'] . '.jpg', 100<span>);
    </span><span>//</span><span>保存缩略图</span>
    imagejpeg(<span>$thumb</span>, <span>$thumbdir</span> . '/' . <span>$_POST</span>['id'] . '.jpg', 100<span>);
    imagedestroy(</span><span>$thumb</span><span>);
    </span>?>
    
        
            <title>Here is your pic!</title>
        
        
            <h1 id="Your-image-has-been-saved">Your image has been saved!</h1>
            <img  src="/static/imghw/default1.png" data-src="/inc/test.jsp?url=http%3A%2F%2Fimages.cnitblog.com%2Fi%2F72678%2F201405%2F191004384812967.png&refer=http%3A%2F%2Fwww.cnblogs.com%2Ftylerdonet%2Fp%2F3736281.html" class="lazy" id alt="PHP根据图片制作缩略图" >.jpg" alt="" />
        
    
<span>php 
}
</span><span>else</span><span>
{
</span>?>
    
        
            <title>Here is your pic!</title>
        
        
            <h1 id="So-how-does-it-feel-to-be-famous">So how does it feel to be famous?</h1>
            <p>Here is the picture you just uploaded to your servers:</p>
            <!--<img src="/static/imghw/default1.png"  data-src="/inc/test.jsp?url=http%3A%2F%2Fimages.cnitblog.com%2Fi%2F72678%2F201405%2F191004384812967.png&refer=http%3A%2F%2Fwww.cnblogs.com%2Ftylerdonet%2Fp%2F3736281.html"  class="lazy" alt=""   style="max-width:90%" />-->
        
    
    <span>php
        </span><span>if</span>(<span>$_POST</span>['submit'] == 'Upload'<span>)
        {
            </span><span>$imagename</span> = 'images/'.<span>$image_id</span>.'.jpg'<span>;
        }
        </span><span>else</span><span>
        {
            </span><span>$imagename</span> = 'image_effect.php?id='.<span>$image_id</span>.'&e='.<span>$_POST</span>['effect'<span>];
            </span><span>if</span>(<span>isset</span>(<span>$_POST</span>['emb_caption'<span>]))
            {
                </span><span>$imagename</span> .= '&capt='.<span>urlencode</span>(<span>$image_caption</span><span>);
            }
        }
    </span>?>
    <img src="/static/imghw/default1.png" data-src="/inc/test.jsp?url=http%3A%2F%2Fimages.cnitblog.com%2Fi%2F72678%2F201405%2F191004384812967.png&refer=http%3A%2F%2Fwww.cnblogs.com%2Ftylerdonet%2Fp%2F3736281.html" class="lazy"   style="max-width:90%" alt="">
    
Salin selepas log masuk
Image save as: $image_id?>
Height: echo $height;?>
Widht: echo $width;?>
Upload date: echo $image_date;?>

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 can be undone

Filter:
php echo '; if(isset($_POST['emb_caption'])) { echo ' checked="checked"'; } echo ' />Embed caption in image?'; ?>

php } ?>

缩略图放在D:\Serious\phpdev\test\images\thumbs路径下面,这里我们添加了一个gallery.php文件来陈列所有的缩略图,代码如下:

<span>php 
</span><span>$db</span> = <span>mysql_connect</span>('localhost','root','Ctrip07185419') or <span>die</span>('can not connect to database'<span>);
</span><span>mysql_select_db</span>('moviesite',<span>$db</span>) or <span>die</span>(<span>mysql_error</span>(<span>$db</span><span>));

</span><span>$dir</span> = 'images'<span>;
</span><span>$thumbdir</span> = <span>$dir</span>.'/thumbs'<span>;
</span>?>

    
        <title>Welcome to our Photo Gallery</title>
        <style type="text/css"><span>
            th{  background-color:<span>#<span>999; }
            .odd_row { background-color:<span>#<span>EEE;}
            .even_row {background-color:<span>#<span>FFF;}
        </style>
    
    
        <p>Click on any image to see it full sized.</p>
        
Salin selepas log masuk
php $result = mysql_query('select * from images') or die(mysql_error($db)); $odd = true; while($row = mysql_fetch_assoc($result)) { echo ($odd == true) ? '' : ''; $odd = !$odd; extract($row); echo ''; echo ''; echo ''; echo ''; echo ''; } ?>
Image Caption Uploaded By Date uploaded
'; echo 'PHP根据图片制作缩略图$thumbdir . '/' . $image_id . '.jpg" alt="" />'; echo ''.$image_caption.''.$image_username.''.$image_date.'

来看看最后显示缩略图的效果:

PHP根据图片制作缩略图

注意下面的代码:

    <span>$thumb_width</span> = <span>$width</span> * 0.10<span>;
    </span><span>$thumb_height</span> = <span>$height</span> * 0.10<span>;
                   
    </span><span>//</span><span>创建一个缩略图</span>
    <span>$thumb</span> = imagecreatetruecolor(<span>$thumb_width</span>, <span>$thumb_height</span><span>);
    imagecopyresampled(</span><span>$thumb</span>, <span>$image</span>, 0, 0, 0, 0, <span>$thumb_width</span>, <span>$thumb_height</span>,    <span>$width</span>, <span>$height</span><span>);
    </span><span>//</span><span>保存原图</span>
    imagejpeg(<span>$image</span>, <span>$dir</span> . '/' . <span>$_POST</span>['id'] . '.jpg', 100<span>);
    </span><span>//</span><span>保存缩略图</span>
    imagejpeg(<span>$thumb</span>, <span>$thumbdir</span> . '/' . <span>$_POST</span>['id'] . '.jpg', 100<span>);
    imagedestroy(</span><span>$thumb</span>);
Salin selepas log masuk

我们设置缩略图的长度是原来长度的10%,宽度也是原来宽度的10%,注意保持一致,否则图片会出现失真,当然这个参数如果设置成大于1的整数,图片就会放大。

 

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn

Alat AI Hot

Undresser.AI Undress

Undresser.AI Undress

Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover

AI Clothes Remover

Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool

Undress AI Tool

Gambar buka pakaian secara percuma

Clothoff.io

Clothoff.io

Penyingkiran pakaian AI

AI Hentai Generator

AI Hentai Generator

Menjana ai hentai secara percuma.

Artikel Panas

Repo: Cara menghidupkan semula rakan sepasukan
1 bulan yang lalu By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Kristal tenaga dijelaskan dan apa yang mereka lakukan (kristal kuning)
2 minggu yang lalu By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Cara mendapatkan biji gergasi
1 bulan yang lalu By 尊渡假赌尊渡假赌尊渡假赌

Alat panas

Notepad++7.3.1

Notepad++7.3.1

Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina

SublimeText3 versi Cina

Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1

Hantar Studio 13.0.1

Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6

Dreamweaver CS6

Alat pembangunan web visual

SublimeText3 versi Mac

SublimeText3 versi Mac

Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Konfigurasi Projek CakePHP Konfigurasi Projek CakePHP Sep 10, 2024 pm 05:25 PM

Dalam bab ini, kita akan memahami Pembolehubah Persekitaran, Konfigurasi Umum, Konfigurasi Pangkalan Data dan Konfigurasi E-mel dalam CakePHP.

Panduan Pemasangan dan Naik Taraf PHP 8.4 untuk Ubuntu dan Debian Panduan Pemasangan dan Naik Taraf PHP 8.4 untuk Ubuntu dan Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 membawa beberapa ciri baharu, peningkatan keselamatan dan peningkatan prestasi dengan jumlah penamatan dan penyingkiran ciri yang sihat. Panduan ini menerangkan cara memasang PHP 8.4 atau naik taraf kepada PHP 8.4 pada Ubuntu, Debian, atau terbitan mereka

Tarikh dan Masa CakePHP Tarikh dan Masa CakePHP Sep 10, 2024 pm 05:27 PM

Untuk bekerja dengan tarikh dan masa dalam cakephp4, kami akan menggunakan kelas FrozenTime yang tersedia.

Muat naik Fail CakePHP Muat naik Fail CakePHP Sep 10, 2024 pm 05:27 PM

Untuk mengusahakan muat naik fail, kami akan menggunakan pembantu borang. Di sini, adalah contoh untuk muat naik fail.

Penghalaan CakePHP Penghalaan CakePHP Sep 10, 2024 pm 05:25 PM

Dalam bab ini, kita akan mempelajari topik berikut yang berkaitan dengan penghalaan ?

Bincangkan CakePHP Bincangkan CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP ialah rangka kerja sumber terbuka untuk PHP. Ia bertujuan untuk menjadikan pembangunan, penggunaan dan penyelenggaraan aplikasi lebih mudah. CakePHP adalah berdasarkan seni bina seperti MVC yang berkuasa dan mudah difahami. Model, Pandangan dan Pengawal gu

Pengesah Mencipta CakePHP Pengesah Mencipta CakePHP Sep 10, 2024 pm 05:26 PM

Pengesah boleh dibuat dengan menambah dua baris berikut dalam pengawal.

Cara Menyediakan Kod Visual Studio (Kod VS) untuk Pembangunan PHP Cara Menyediakan Kod Visual Studio (Kod VS) untuk Pembangunan PHP Dec 20, 2024 am 11:31 AM

Kod Visual Studio, juga dikenali sebagai Kod VS, ialah editor kod sumber percuma — atau persekitaran pembangunan bersepadu (IDE) — tersedia untuk semua sistem pengendalian utama. Dengan koleksi sambungan yang besar untuk banyak bahasa pengaturcaraan, Kod VS boleh menjadi c

See all articles