热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

php完整图片按比例生成缩略图代码-PHP源码

ec(2);<?phprequire(resize_img.php);howtousetheclass:makesasimplethumbnailofanimageof100x100andsavestheimagethenoutputsit.$imgresizenewresize_img();$imgresize

require ('resize_img.php');
//how to use the class:
//makes a simple thumbnail of an image of 100x100 and saves the image then outputs it.
$imgresize = new resize_img();

$imgresize->sizelimit_x = 100;
$imgresize->sizelimit_y = 100;
$imgresize->keep_proportiOns= true;
$imgresize->output = 'PNG';

if( $imgresize->resize_image( 'C:/xampp/htdocs/wannabe/image_bin/public/treeshadows_001.gif' ) === false )
{
echo 'ERROR!';
}
else
{
$imgresize->save_resizedimage( 'C:/xampp/htdocs/wannabe/image_bin/public/thumbnails/', 'treeshadows_001' );
$imgresize->output_resizedimage();
}

$imgresize->destroy_resizedimage();

?>

// resize_img.php文件

/**
* by Daniel M. Story
*/

class resize_img
{
var $image_path = '';
//holds the image path
var $sizelimit_x = 250;
//the limit of the image width
var $sizelimit_y = 250;
//the limit of the image height
var $image_resource = '';
//holds the image resource
var $keep_proportiOns= true;
//if true it keeps教程 the image proportions when resized
var $resized_resource = '';
//holds the resized image resource
var $hasGD = false;
var $output = 'SAME';
//can be JPG, GIF, PNG, or SAME (same will save as old type)

function resize_img()
{
if( function_exists('gd_info') ){ $this->hasGD = true; }
}

function resize_image( $image_path )
{
if( $this->hasGD === false ){ return false; }
//no GD installed on the server!

list($img_width, $img_height, $img_type, $img_attr) = @getimagesize( $image_path );
//this is going to get the image width, height, and format

if( ( $img_width != 0 ) || ( $img_width != 0 ) )
//make sure it was loaded correctly
{
switch( $img_type )
{
case 1:
//GIF
$this->image_resource = @imagecreatefromgif( $image_path );
if( $this->output == 'SAME' ){ $this->output = 'GIF'; }
break;
case 2:
//JPG
$this->image_resource = @imagecreatefromjpeg( $image_path );
if( $this->output == 'SAME' ){ $this->output = 'JPG'; }
break;
case 3:
//PNG
$this->image_resource = @imagecreatefrompng( $image_path );
if( $this->output == 'SAME' ){ $this->output = 'PNG'; }
}
if( $this->image_resource === '' ){ return false; }
//it wasn't able to load the image
}
else{ return false; }
//something happened!

if( $this->keep_proportiOns=== true )
{
if( ($img_width-$this->sizelimit_x) > ($img_height-$this->sizelimit_y) )
{
//if the width of the img is greater than the size limit we scale by width
$scalex = ( $this->sizelimit_x / $img_width );
$scaley = $scalex;
}
else
//if the height of the img is greater than the size limit we scale by height
{
$scalex = ( $this->sizelimit_y / $img_height );
$scaley = $scalex;
}

}
else
{
$scalex = ( $this->sizelimit_x / $img_width );
$scaley = ( $this->sizelimit_y / $img_height );
//just make the image fit the image size limit

if( $scalex > 1 ){ $scalex = 1; }
if( $scaley > 1 ){ $scaley = 1; }
//don't make it so it streches the image
}

$new_width = $img_width * $scalex;
$new_height = $img_height * $scaley;

$this->resized_resource = @imagecreatetruecolor( $new_width, $new_height );
//creates an image resource, with the width and height of the size limits (or new resized proportion )

if( function_exists( 'imageantialias' )){ @imageantialias( $this->resized_resource, true ); }
//helps in the quality of the image being resized

@imagecopyresampled( $this->resized_resource, $this->image_resource, 0, 0, 0, 0, $new_width, $new_height, $img_width, $img_height );
//resize the iamge onto the resized resource

@imagedestroy( $this->image_resource );
//destory old image resource

return true;
}

function save_resizedimage( $path, $name )
{
switch( strtoupper($this->output) )
{
case 'GIF':
//GIF
@imagegif( $this->resized_resource, $path . $name . '.gif' );
break;
case 'JPG':
//JPG
@imagejpeg( $this->resized_resource, $path . $name . '.jpg' );
break;
case 'PNG':
//PNG
@imagepng( $this->resized_resource, $path . $name . '.png' );
}
}

function output_resizedimage()
{
$the_time = time();
header('Last-Modified: ' . date( 'D, d M Y H:i:s', $the_time ) . ' GMT');
header('Cache-Control: public');

switch( strtoupper($this->output) )
{
case 'GIF':
//GIF
header('Content-type: image/gif');
@imagegif( $this->resized_resource );
break;
case 'JPG':
//JPG
header('Content-type: image/jpg');
@imagejpeg( $this->resized_resource );
break;
case 'PNG':
//PNG
header('Content-type: image/png');
@imagepng( $this->resized_resource );
}
}

function destroy_resizedimage()
{
@imagedestroy( $this->resized_resource );
@imagedestroy( $this->image_resource );
}

}

?>


推荐阅读
  • 本文介绍了如何使用PHP向系统日历中添加事件的方法,通过使用PHP技术可以实现自动添加事件的功能,从而实现全局通知系统和迅速记录工具的自动化。同时还提到了系统exchange自带的日历具有同步感的特点,以及使用web技术实现自动添加事件的优势。 ... [详细]
  • 2018年人工智能大数据的爆发,学Java还是Python?
    本文介绍了2018年人工智能大数据的爆发以及学习Java和Python的相关知识。在人工智能和大数据时代,Java和Python这两门编程语言都很优秀且火爆。选择学习哪门语言要根据个人兴趣爱好来决定。Python是一门拥有简洁语法的高级编程语言,容易上手。其特色之一是强制使用空白符作为语句缩进,使得新手可以快速上手。目前,Python在人工智能领域有着广泛的应用。如果对Java、Python或大数据感兴趣,欢迎加入qq群458345782。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • vue使用
    关键词: ... [详细]
  • 一、Hadoop来历Hadoop的思想来源于Google在做搜索引擎的时候出现一个很大的问题就是这么多网页我如何才能以最快的速度来搜索到,由于这个问题Google发明 ... [详细]
  • 在Docker中,将主机目录挂载到容器中作为volume使用时,常常会遇到文件权限问题。这是因为容器内外的UID不同所导致的。本文介绍了解决这个问题的方法,包括使用gosu和suexec工具以及在Dockerfile中配置volume的权限。通过这些方法,可以避免在使用Docker时出现无写权限的情况。 ... [详细]
  • 学习SLAM的女生,很酷
    本文介绍了学习SLAM的女生的故事,她们选择SLAM作为研究方向,面临各种学习挑战,但坚持不懈,最终获得成功。文章鼓励未来想走科研道路的女生勇敢追求自己的梦想,同时提到了一位正在英国攻读硕士学位的女生与SLAM结缘的经历。 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了brain的意思、读音、翻译、用法、发音、词组、同反义词等内容,以及脑新东方在线英语词典的相关信息。还包括了brain的词汇搭配、形容词和名词的用法,以及与brain相关的短语和词组。此外,还介绍了与brain相关的医学术语和智囊团等相关内容。 ... [详细]
  • 云原生边缘计算之KubeEdge简介及功能特点
    本文介绍了云原生边缘计算中的KubeEdge系统,该系统是一个开源系统,用于将容器化应用程序编排功能扩展到Edge的主机。它基于Kubernetes构建,并为网络应用程序提供基础架构支持。同时,KubeEdge具有离线模式、基于Kubernetes的节点、群集、应用程序和设备管理、资源优化等特点。此外,KubeEdge还支持跨平台工作,在私有、公共和混合云中都可以运行。同时,KubeEdge还提供数据管理和数据分析管道引擎的支持。最后,本文还介绍了KubeEdge系统生成证书的方法。 ... [详细]
  • Echarts图表重复加载、axis重复多次请求问题解决记录
    文章目录1.需求描述2.问题描述正常状态:问题状态:3.解决方法1.需求描述使用Echats实现了一个中国地图:通过选择查询周期&#x ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 本文介绍了数据库的存储结构及其重要性,强调了关系数据库范例中将逻辑存储与物理存储分开的必要性。通过逻辑结构和物理结构的分离,可以实现对物理存储的重新组织和数据库的迁移,而应用程序不会察觉到任何更改。文章还展示了Oracle数据库的逻辑结构和物理结构,并介绍了表空间的概念和作用。 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
author-avatar
希臘神話2502873813
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有