PHP and JS to the database image scaling sample _javascript tips

Source: Internet
Author: User
JS Zoom on a picture's equal ratio

Code
Copy Code code 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 ">

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> latest JavaScript automatically proportional display of pictures, compress pictures by proportional display </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>

<body>
<br/>
Original artwork display (534 X) <br/>
Onload= "Autoresizeimage (0,0,this) <br/>
<a href= "./img/img_20140424_200722.jpg" target= "_blank" ></a><br/><br/>
3. According to the height 250 width 250 by the proportional compression <br/>
Onload= "Autoresizeimage (250,250,this)" <br/>
<a href= "./img/img_20140424_200722.jpg" target= "_blank" ></a><br/><br
6. If the original height and width of the picture is less than the maximum height and width of compression, the display picture will not be pulled (as shown) <br/>
The original 444 x 207, compressed to x 600, will keep the original image display <br/>
Onload= "Autoresizeimage (500,600,this)" <br/>
<a href= "./img/img_20140424_200722.jpg" target= "_blank" ></a><br/><br/>
</body>


PHP's equal scaling for database pictures
Copy Code code as follows:

<?php
Class imgsf{
function make_img ($img _address) {
Picture's equal scaling

Because PHP can only operate on resources, copy the picture that needs to be scaled to create a new resource
$src =imagecreatefromjpeg ($img _address);

Get the width and height of the source picture
$size _src=getimagesize ($img _address);
$w = $size _src[' 0 '];
$h = $size _src[' 1 '];

Specifies the maximum width to zoom out (and possibly the height)
$max = 300;

Calculates the length of the other side based on the maximum value of 300, and gets the scaled picture 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 $w wide, $h high true Color picture resources
$image =imagecreatetruecolor ($w, $h);


Key functions, parameters (target resource, source, target resource start coordinate x,y, source resource start coordinate x,y, target resource's width high w,h, source resource's width high w,h)
Imagecopyresampled ($image, $src, 0, 0, 0, 0, $w, $h, $size _src[' 0 '], $size _src[' 1 ']);

Tell the browser to parse it in the form of a picture
Header (' content-type:image/png ');
Imagepng ($image);

Destroying resources
Imagedestroy ($image);
}
}
$obj =new imgsf ();
$obj->make_img ("./img/img_20140424_200722.jpg");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.