کدی هست که بشه باهاش تصاویر رو ویرایش کرد؟!
مثلا طول و عرض هر عکسی رو که آپلود میشه به یه مقدار خاص تغییر بده!
Printable View
کدی هست که بشه باهاش تصاویر رو ویرایش کرد؟!
مثلا طول و عرض هر عکسی رو که آپلود میشه به یه مقدار خاص تغییر بده!
سلام. بله هست.
اول این کلاس رو لود کنید یا توی همون اسکریپتتون بنویسیدش:
کد:<?php
class ResizeImage
{
private $ext;
private $image;
private $newImage;
private $origWidth;
private $origHeight;
private $resizeWidth;
private $resizeHeight;
/**
* Class constructor requires to send through the image filename
*
* @ [ برای مشاهده لینک ، با نام کاربری خود وارد شوید یا ثبت نام کنید ] string $filename - Filename of the image you want to resize
*/
public function __construct( $filename )
{
if(file_exists($filename))
{
$this->setImage( $filename );
} else {
throw new Exception('Image ' . $filename . ' can not be found, try another image.');
}
}
/**
* Set the image variable by using image create
*
* @ [ برای مشاهده لینک ، با نام کاربری خود وارد شوید یا ثبت نام کنید ] string $filename - The image filename
*/
private function setImage( $filename )
{
$size = getimagesize($filename);
$this->ext = $size['mime'];
switch($this->ext)
{
// Image is a JPG
case 'image/jpg':
case 'image/jpeg':
// create a jpeg extension
$this->image = imagecreatefromjpeg($filename);
break;
// Image is a GIF
case 'image/gif':
$this->image = @ [ برای مشاهده لینک ، با نام کاربری خود وارد شوید یا ثبت نام کنید ] createfromgif($filename);
break;
// Image is a PNG
case 'image/png':
$this->image = @ [ برای مشاهده لینک ، با نام کاربری خود وارد شوید یا ثبت نام کنید ] createfrompng($filename);
break;
// Mime type not found
default:
throw new Exception("File is not an image, please use another file type.", 1);
}
$this->origWidth = imagesx($this->image);
$this->origHeight = imagesy($this->image);
}
/**
* Save the image as the image type the original image was
*
* @ [ برای مشاهده لینک ، با نام کاربری خود وارد شوید یا ثبت نام کنید ] String[type] $savePath - The path to store the new image
* @ [ برای مشاهده لینک ، با نام کاربری خود وارد شوید یا ثبت نام کنید ] string $imageQuality - The qulaity level of image to create
*
* @ [ برای مشاهده لینک ، با نام کاربری خود وارد شوید یا ثبت نام کنید ] Saves the image
*/
public function saveImage($savePath, $imageQuality="100", $download = false)
{
switch($this->ext)
{
case 'image/jpg':
case 'image/jpeg':
// Check PHP supports this file type
if (imagetypes() & IMG_JPG) {
imagejpeg($this->newImage, $savePath, $imageQuality);
}
break;
case 'image/gif':
// Check PHP supports this file type
if (imagetypes() & IMG_GIF) {
imagegif($this->newImage, $savePath);
}
break;
case 'image/png':
$invertScaleQuality = 9 - round(($imageQuality/100) * 9);
// Check PHP supports this file type
if (imagetypes() & IMG_PNG) {
imagepng($this->newImage, $savePath, $invertScaleQuality);
}
break;
}
if($download)
{
header('Content-Description: File Transfer');
header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename= ".$savePath."");
readfile($savePath);
}
imagedestroy($this->newImage);
}
/**
* Resize the image to these set dimensions
*
* @ [ برای مشاهده لینک ، با نام کاربری خود وارد شوید یا ثبت نام کنید ] int $width - Max width of the image
* @ [ برای مشاهده لینک ، با نام کاربری خود وارد شوید یا ثبت نام کنید ] int $height - Max height of the image
* @ [ برای مشاهده لینک ، با نام کاربری خود وارد شوید یا ثبت نام کنید ] string $resizeOption - Scale option for the image
*
* @ [ برای مشاهده لینک ، با نام کاربری خود وارد شوید یا ثبت نام کنید ] Save new image
*/
public function resizeTo( $width, $height, $resizeOption = 'default' )
{
switch(strtolower($resizeOption))
{
case 'exact':
$this->resizeWidth = $width;
$this->resizeHeight = $height;
break;
case 'maxwidth':
$this->resizeWidth = $width;
$this->resizeHeight = $this->resizeHeightByWidth($width);
break;
case 'maxheight':
$this->resizeWidth = $this->resizeWidthByHeight($height);
$this->resizeHeight = $height;
break;
default:
if($this->origWidth > $width || $this->origHeight > $height)
{
if ( $this->origWidth > $this->origHeight ) {
$this->resizeHeight = $this->resizeHeightByWidth($width);
$this->resizeWidth = $width;
} else if( $this->origWidth < $this->origHeight ) {
$this->resizeWidth = $this->resizeWidthByHeight($height);
$this->resizeHeight = $height;
} else {
$this->resizeWidth = $width;
$this->resizeHeight = $height;
}
} else {
$this->resizeWidth = $width;
$this->resizeHeight = $height;
}
break;
}
$this->newImage = imagecreatetruecolor($this->resizeWidth, $this->resizeHeight);
imagecopyresampled($this->newImage, $this->image, 0, 0, 0, 0, $this->resizeWidth, $this->resizeHeight, $this->origWidth, $this->origHeight);
}
/**
* Get the resized height from the width keeping the aspect ratio
*
* @ [ برای مشاهده لینک ، با نام کاربری خود وارد شوید یا ثبت نام کنید ] int $width - Max image width
*
* @ [ برای مشاهده لینک ، با نام کاربری خود وارد شوید یا ثبت نام کنید ] Height keeping aspect ratio
*/
private function resizeHeightByWidth($width)
{
return floor(($this->origHeight/$this->origWidth)*$width);
}
/**
* Get the resized width from the height keeping the aspect ratio
*
* @ [ برای مشاهده لینک ، با نام کاربری خود وارد شوید یا ثبت نام کنید ] int $height - Max image height
*
* @ [ برای مشاهده لینک ، با نام کاربری خود وارد شوید یا ثبت نام کنید ] Width keeping aspect ratio
*/
private function resizeWidthByHeight($height)
{
return floor(($this->origWidth/$this->origHeight)*$height);
}
}
?>
نحوه استفاده ازش هم به شکل زیره:
کد:$resize = new ResizeImage('images/Be-Original.jpg');
$resize->resizeTo(100, 100, 'exact');
$resize->saveImage('images/be-original-exact.jpg');
کارت خیلی درسته دستت درد نکنه!
ولی اگه بخوام نحوه ی کارشو بفهمم باید حتما php object oriented یاد بگیرم!؟
ار همین کدی که من دادم بخوای فقط استفاده کنی که نه، نحوه استفاده اش رو هم من توضیح دادم همونا رو کپی کن و آدرس فایل عکس خودتو بده فقط.
ولی در کل بدون OOP کارت خیلی زاره! حتما باید یاد بگیری اگه بلد نیستی. اینجا هم قبلا یادمه یکی از بچه ها آموزشش رو گذاشته بود: [ برای مشاهده لینک ، با نام کاربری خود وارد شوید یا ثبت نام کنید ]