تغيير اندازه عکس با Php

sabasoft

Member
سلام دوستان
راهنمايي براي اينکه چه کدي و چجوري هست که ميشه فايل هاي عکس را تغيير اندازه داد به صورتيکه انگار اون عکس خودش همين اندازه هست؟
يکي براي زماني که عکس را مي خواهيم آپلود کنيم يک کپي از عکس براي Thumbnail از همون عکس
و يکي ديگه هم بدون اينکه قصد آپلود داشته باشيم ، يک عکس با يک آدرس را تغيير سايز دهيم

اميدوارم تونسته باشم بفهمونم که چي مي خوام
 

peyman1987

Member
شما باید با اکستنشن gd کار کنین. کد زیر نمونه یه کده که با دادن مسیر عکس ، thumbnail اون رو بهتون میده. در ضمن اگه جستجو کنین خیلی مطلب در این باره نوشته شده.

PHP:
<?php
$filename = "Pictures/".$_GET['img'];
$intWidth = $_GET['width'] + 0;
$intHeight = $_GET['height'] + 0;
$percent = $_GET['percent'] + 0;

HEADER('Content-type: image/jpeg');

LIST($width, $height) = getimagesize($filename);

IF($intWidth != 0 AND $intHeight != 0)
{
	$newwidth = $intWidth;
	$newheight = $intHeight;
}
ELSE
{
	$newwidth = $width * $percent;
	$newheight = $height * $percent;
}

$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

imagejpeg($thumb);
?>

اینم طرز استفاده ش :
HTML:
<IMG Src="Thumbnail.php?width=20&height=20&img=a.jpg">
 

parsmizban

Member
سلام
این یکم حرفه ای تره
PHP:
<?php
/**
 * @copyright 2005 Alessandro Pasotti 
 * @license GPL
 * @author  Alessandro Pasotti (some source grabbed somewhere)
 *
 * Read an image file and send a scaled thumbnail to the browser
 * default to 150 px width.
 *
 * This file can also be included, in this case, define vars in the calling script
 *
 * @param   string  $img_path image file path, relative to this script, if not
 *                            set, the next is checked on GET
 * @param   string  $img urlencoded image file path, relative to this script
 *                      (in vncms should be 2 level depth)
 * @param   integer $width max width
 * @param   integer $height max height
 * @param   string  $jpgfilename optional filename to store image in, if
 *                            not given, image is outputted to the browser
 *
 *
 * Example calls:
 *
 * getthumb.php?img=../../images/logo.jpg              --> default to max 150 (w or h)
 * getthumb.php?img=../../images/logo.jpg&height=200   --> resize to 200px height
 *
 *
 */
 
 
// Constants
define('DEF_WIDTH',     150);  // default
define('DEF_HEIGHT',    150);  // default
define('ALLOWED_WIDTH', 1024); // no tampering: set up a max
define('ALLOWED_HEIGHT',768);  // no tampering: set up a max
define('DEF_QUALITY',   90);   // default quality
 
define('DEBUG', false);
 
 
// I put this here to avoid any external dependency
if(!function_exists('__clean')) {
  function __clean($s, $len){
      if(!isset($s)) return null;
      $s = substr($s, 0, $len);
      if(ini_get('gpc_magic_quotes') != 1) $s = addslashes($s);
      $s = escapeshellcmd($s);
      return $s;
  }
}
 
// Get image location if not defined elsewhere
if(!isset($image_path))  $image_path = urldecode(stripslashes(__clean($_GET['img'], 256)));
if(!isset($width))       $width      = __clean($_GET['width'], 4);
if(!isset($height))      $height     = __clean($_GET['height'], 4);
 
// if both are not defined:
if(!$width && !$height) {
  $width = DEF_WIDTH;
  $height= DEF_HEIGHT;
}
 
if($width) $width   =  min(ALLOWED_WIDTH, $width);
else $width = ALLOWED_WIDTH;
if($height) $height =  min(ALLOWED_HEIGHT, $height);
else $height = ALLOWED_HEIGHT;
 
 
define('MAX_HEIGHT', $height);
define('MAX_WIDTH' , $width);
 
 
// Load image
$img = null;
$imginfo = getimagesize($image_path);
$imgtype = $imginfo[2];
 
$ext = strtolower(end(explode('.', $image_path)));
if ($imgtype == IMAGETYPE_JPEG) {
    $img = @imagecreatefromjpeg($image_path);
} else if ($imgtype == IMAGETYPE_PNG) {
    $img = @imagecreatefrompng($image_path);
    //echo "IMGEBASE " . IMAGE_BASE . ' path: ' .  $image_path; exit;
// Only if your version of GD includes GIF support
} else if ($imgtype == IMAGETYPE_GIF) {
    $img = @imagecreatefromgif($image_path);
}
 
// If an image was successfully loaded, test the image for size
if ($img) {
 
    // Get image size and scale ratio
    $width = imagesx($img);
    $height = imagesy($img);
    $scale = min(MAX_WIDTH/$width, MAX_HEIGHT/$height);
    if(defined('DEBUG') && DEBUG) {
      user_error("thumbnails.php - IMAGE: $image_path  WIDTH: $width HEIGHT: $height SCALE: $scale<hr />");
      exit;
    }
 
    // If the image is larger than max shrink it
    if ($scale < 1) {
        $new_width = floor($scale*$width);
        $new_height = floor($scale*$height);
 
        // Create a new temporary image
        $tmp_img = imagecreatetruecolor($new_width, $new_height);
 
        // Copy and resize old image into new image
        imagecopyresized($tmp_img, $img, 0, 0, 0, 0,
                         $new_width, $new_height, $width, $height);
        imagedestroy($img);
        $img = $tmp_img;
    }
}
 
// Create error image if necessary
if (!$img) {
    $img = imagecreate(DEF_WIDTH, DEF_HEIGHT);
    imagecolorallocate($img,255,255,255);
    $c2 = imagecolorallocate($img,255,0,0);
    imageline($img,0,0,DEF_WIDTH,DEF_HEIGHT,$c2);
    imageline($img,DEF_WIDTH,0,0,DEF_HEIGHT,$c2);
}
 
if(!$jpgfilename) {
  // Display the image
  header("Content-type: image/jpg");
  imagejpeg($img, null, DEF_QUALITY);
  exit;
} else {
   if(defined('DEBUG') && DEBUG) {
      echo "thumbnails.php - saving to $jpgfilename<hr>";
  }
  imagejpeg($img, $jpgfilename, DEF_QUALITY);
}
?>
موفق باشید
 

sabasoft

Member
دوستان يک مشکلي هست (بدون شک از کم علمي منه)
اونم اينه که من تلاش مي کنم تو localhost اجرا بشه که اجرا نميشه نميدونم ، راهنماييم کنيد

ممنونم
 

peyman1987

Member
باید اکستنشن gd رو فعال کنین.
اگر از php 5 استفاده میکنین ، فایل php.ini رو باز کنین ، سمی کالن اول خط ;extension gd.dll رو بردارین ، فایل رو ذخیره کنین و آپاچی رو ریستارت کنین حل میشه.
 

sabasoft

Member
ميرم تغييرات رو بدم ببينم چي ميشه

ممنون از راهنماييتون


---
راستي اگه php 5 نبود چي؟
 
آخرین ویرایش:

biography

New Member
باید اکستنشن gd رو فعال کنین.
اگر از php 5 استفاده میکنین ، فایل php.ini رو باز کنین ، سمی کالن اول خط ;extension gd.dll رو بردارین ، فایل رو ذخیره کنین و آپاچی رو ریستارت کنین حل میشه.

من هر چي گشتم نتونستم اين ;extension gd.dll رو پيدا كنم
نمي‌دونم شايد مشكل از سيستمي باشه كه من نصب كردم.
من براي لوكال از Wamp5 استفاده مي‌كنم
 

peyman1987

Member
خوب اگه از ومپ استفاده میکنین روی آیکونش توی سیستم تری کلبک کنین و توی بخش php extensions ( یه چیزی توی این مایه ها ) 'گزینه GD2 رو تیک بزنین بعدش هم ومپ رو ریستارت کنین.
 

جدیدترین ارسال ها

بالا