Home Web Front-end JS Tutorial Example of proportional scaling of database images with php and js_javascript skills

Example of proportional scaling of database images with php and js_javascript skills

May 16, 2016 pm 04:50 PM

JS proportional scaling of an image

Code
Copy code The code is as follows:

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional/ /EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml ">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Latest javascript Automatically display images proportionally and compress images proportionally</title>
<script type="text/javascript">
function AutoResizeImage(maxWidth,maxHeight,objImg){
var img = new Image();
img.src = objImg.src;
var hRatio;
var wRatio;
var Ratio = 1;
var w = img.width;
var h = img.height;
wRatio = maxWidth / w;
hRatio = maxHeight / h;
if (maxWidth ==0 && maxHeight==0){
Ratio = 1;
}else if (maxWidth==0){//
if (hRatio<1) Ratio = hRatio;
}else if (maxHeight==0){
if (wRatio<1) Ratio = wRatio ;
}else if (wRatio<1 || hRatio<1){
Ratio = (wRatio<=hRatio?wRatio:hRatio);
}
if (Ratio<1){
w = w * Ratio;
h = h * Ratio;
}
objImg.height = h;
objImg.width = w;
}
</script>
</head>
<body>
<br />
Original image display (534 X 800)<br />
onload="AutoResizeImage(0 ,0,this)<br />
<a href="./img/IMG_20140424_200722.jpg" target="_blank"><img src="./img/IMG_20140424_200722.jpg" border ="0" width="0" height="0" onload="AutoResizeImage(0,0,this)" alt="534 X 800"/></a><br/><br /> ./img/IMG_20140424_200722.jpg" target="_blank"><img src="./img/IMG_20140424_200722.jpg" border="0" width="0" height="0" onload="AutoResizeImage(250,250 ,this)" alt="200 , the image will not be enlarged and displayed (displayed according to the original image)<br />
The original image is 444 x 207, compressed to 500 x 600, and the original image will be displayed<br />
onload ="AutoResizeImage(500,600,this)"<br />
<a href="./img/IMG_20140424_200722.jpg" target="_blank"><img src="./img/IMG_20140424_200722 .jpg" border="0" width="0" height="0" onload="AutoResizeImage(500,600,this)" alt="444 X 207"/></a><br />&lt ;br />
</body>
</html>



PHP proportional scaling of database images


Copy code
The code is as follows: <?php class ImgSF{
function make_img($img_address ){
//Constantial scaling of pictures

//Because PHP can only operate on resources, you need to copy the pictures that need to be scaled and create them as new resources
$src =imagecreatefromjpeg($img_address);

//Get the width and height of the source image
$size_src=getimagesize($img_address);
$w=$size_src['0'];
$h=$size_src['1'];

//Specify the maximum width (maybe height) of scaling
$max=300;

//According to The maximum value is 300, calculate the length of the other side, and get the scaled image width and height
if($w > $h){
$w=$max;
$h=$h *($max/$size_src['0']);
}else{
$h=$max;
$w=$w*($max/$size_src['1']) ;
}


//Declare a true color image resource of $w width and $h height
$image=imagecreatetruecolor($w, $h);


//Key function, parameters (target resource, source, starting coordinates x,y of the target resource, starting coordinates x,y of the source resource, width and height w,h of the target resource, width and height w of the source resource, h)
imagecopyresampled($image, $src, 0, 0, 0, 0, $w, $h, $size_src['0'], $size_src['1']);

//Tell the browser to parse
header('content-type:image/png');
imagepng($image);

//Destroy resources
imagedestroy($ image);
}
}
$obj=new ImgSF();
$obj->make_img("./img/IMG_20140424_200722.jpg");

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Replace String Characters in JavaScript

jQuery get element padding/margin jQuery get element padding/margin Mar 01, 2025 am 08:53 AM

jQuery get element padding/margin

jQuery Check if Date is Valid jQuery Check if Date is Valid Mar 01, 2025 am 08:51 AM

jQuery Check if Date is Valid

10 jQuery Accordions Tabs 10 jQuery Accordions Tabs Mar 01, 2025 am 01:34 AM

10 jQuery Accordions Tabs

10 Worth Checking Out jQuery Plugins 10 Worth Checking Out jQuery Plugins Mar 01, 2025 am 01:29 AM

10 Worth Checking Out jQuery Plugins

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

Custom Google Search API Setup Tutorial

HTTP Debugging with Node and http-console HTTP Debugging with Node and http-console Mar 01, 2025 am 01:37 AM

HTTP Debugging with Node and http-console

jquery add scrollbar to div jquery add scrollbar to div Mar 01, 2025 am 01:30 AM

jquery add scrollbar to div

See all articles