note 110187 added to function.imagecopyresized
| From: | zen at gmail dot com | Date: | Thu, 27 Sep 2012 23:23:44 +0000 |
| Subject: | note 110187 added to function.imagecopyresized | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-191050@lists.php.net to get a copy of this message | ||
<?php
class SimpleImage {
var $image;
var $image_type;
function load($filename) {
$image_info = getimagesize($filename);
$this->image_type = $image_info[2];
if( $this->image_type == IMAGETYPE_JPEG ) {
$this->image = imagecreatefromjpeg($filename);
} elseif( $this->image_type == IMAGETYPE_GIF ) {
$this->image = imagecreatefromgif($filename);
} elseif( $this->image_type == IMAGETYPE_PNG ) {
$this->image = imagecreatefrompng($filename);
}
}
function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image,$filename,$compression);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image,$filename);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image,$filename);
}
if( $permissions != null) {
chmod($filename,$permissions);
}
}
function output($image_type=IMAGETYPE_JPEG) {
if( $image_type == IMAGETYPE_JPEG ) {
imagejpeg($this->image);
} elseif( $image_type == IMAGETYPE_GIF ) {
imagegif($this->image);
} elseif( $image_type == IMAGETYPE_PNG ) {
imagepng($this->image);
}
}
function getWidth() {
return imagesx($this->image);
}
function getHeight() {
return imagesy($this->image);
}
function resizeToHeight($height) {
$ratio = $height / $this->getHeight();
$width = $this->getWidth() * $ratio;
$this->resize($width,$height);
}
function resizeToWidth($width) {
$ratio = $width / $this->getWidth();
$height = $this->getheight() * $ratio;
$this->resize($width,$height);
}
function scale($scale) {
$width = $this->getWidth() * $scale/100;
$height = $this->getheight() * $scale/100;
$this->resize($width,$height);
}
function resize($width,$height) {
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height,
$this->getWidth(), $this->getHeight());
$this->image = $new_image;
}
}
?>
<?php
if( isset($_POST['submit']) ) {
include('SimpleImage.php');
$image = new SimpleImage();
$image->load($_FILES['uploaded_image']['tmp_name']);
$image->resizeToWidth(150);
$image->output();
} else {
?>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<input type="file" name="uploaded_image" />
<input type="submit" name="submit" value="Upload" />
</form>
<?php
}
?>
----
Server IP: 69.147.83.199
Probable Submitter: 192.135.209.8
----
Manual Page -- http://www.php.net/manual/en/function.imagecopyresized.php
Edit -- https://master.php.net/note/edit/110187
Del: integrated -- https://master.php.net/note/delete/110187/integrated
Del: useless -- https://master.php.net/note/delete/110187/useless
Del: bad code -- https://master.php.net/note/delete/110187/bad+code
Del: spam -- https://master.php.net/note/delete/110187/spam
Del: non-english -- https://master.php.net/note/delete/110187/non-english
Del: in docs -- https://master.php.net/note/delete/110187/in+docs
Del: other reasons-- https://master.php.net/note/delete/110187
Reject -- https://master.php.net/note/reject/110187
Search -- https://master.php.net/manage/user-notes.php