PDA

View Full Version : ریسایز و کم حجم کردن تصاویر در php



nokhodi
شنبه 26 مهر 1393, 08:47 صبح
سلام دوستان
آیا امکانش هست که با دستورات php عکسی رو ریسایز و کم حجم کرد و بعد از اعمال تغییرات اونو به عنوان تصویر جدید در هاست ذخیره کرد؟

foad.fartash
شنبه 26 مهر 1393, 10:07 صبح
سلام بله همچین چیزی هست.
function create_cropped_thumbnail($image_path, $thumb_width, $thumb_height, $prefix) {

if (!(is_integer($thumb_width) && $thumb_width > 0) && !($thumb_width === "*")) {
echo "The width is invalid";
exit(1);
}

if (!(is_integer($thumb_height) && $thumb_height > 0) && !($thumb_height === "*")) {
echo "The height is invalid";
exit(1);
}

$extension = pathinfo($image_path, PATHINFO_EXTENSION);
switch ($extension) {
case "jpg":
case "jpeg":
$source_image = imagecreatefromjpeg($image_path);
break;
case "gif":
$source_image = imagecreatefromgif($image_path);
break;
case "png":
$source_image = imagecreatefrompng($image_path);
break;
default:
exit(1);
break;
}

$source_width = imageSX($source_image);
$source_height = imageSY($source_image);

if (($source_width / $source_height) == ($thumb_width / $thumb_height)) {
$source_x = 0;
$source_y = 0;
}

if (($source_width / $source_height) > ($thumb_width / $thumb_height)) {
$source_y = 0;
$temp_width = $source_height * $thumb_width / $thumb_height;
$source_x = ($source_width - $temp_width) / 2;
$source_width = $temp_width;
}

if (($source_width / $source_height) < ($thumb_width / $thumb_height)) {
$source_x = 0;
$temp_height = $source_width * $thumb_height / $thumb_width;
$source_y = ($source_height - $temp_height) / 2;
$source_height = $temp_height;
}

$target_image = ImageCreateTrueColor($thumb_width, $thumb_height);

imagecopyresampled($target_image, $source_image, 0, 0, $source_x, $source_y, $thumb_width, $thumb_height, $source_width, $source_height);

switch ($extension) {
case "jpg":
case "jpeg":
imagejpeg($target_image, $prefix . "_" . $image_path);
break;
case "gif":
imagegif($target_image, $prefix . "_" . $image_path);
break;
case "png":
imagepng($target_image, $prefix . "_" . $image_path);
break;
default:
exit(1);
break;
}

imagedestroy($target_image);
imagedestroy($source_image);
}

nokhodi
شنبه 26 مهر 1393, 10:14 صبح
ممنون منم اینو تو سرچ پیدا کردم :




<?PHP
function imagecrop($img_name,$newname,$type,$modwidth,$modh eight)
{

list (http://www.php.net/list)($width, $height) = getimagesize (http://www.php.net/getimagesize)($img_name) ; //get width & height in array list

$tn = imagecreatetruecolor (http://www.php.net/imagecreatetruecolor)($modwidth, $modheight);
if(!strcmp (http://www.php.net/strcmp)("image/png",$type))
{
imagealphablending (http://www.php.net/imagealphablending)($tn, false); //For transparent BackGround
imagesavealpha (http://www.php.net/imagesavealpha)($tn, true);
}



if(!strcmp (http://www.php.net/strcmp)("image/jpg",$type) || !strcmp (http://www.php.net/strcmp)("image/jpeg",$type) || !strcmp (http://www.php.net/strcmp)("image/pjpeg",$type))
$src_img=imagecreatefromjpeg (http://www.php.net/imagecreatefromjpeg)($img_name);

if(!strcmp (http://www.php.net/strcmp)("image/png",$type))
$src_img=imagecreatefrompng (http://www.php.net/imagecreatefrompng)($img_name);

if(!strcmp (http://www.php.net/strcmp)("image/gif",$type))
$src_img=imagecreatefromgif (http://www.php.net/imagecreatefromgif)($img_name);

imagecopyresampled (http://www.php.net/imagecopyresampled)($tn, $src_img, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;


if(!strcmp (http://www.php.net/strcmp)("image/png",$type))
{
imagesavealpha (http://www.php.net/imagesavealpha)($src_img, true);
$ok=imagepng (http://www.php.net/imagepng)($tn,$newname);
}
else if(!strcmp (http://www.php.net/strcmp)("image/gif",$type))
{
$ok=imagegif (http://www.php.net/imagegif)($tn,$newname);
}
else
{
$ok=imagejpeg (http://www.php.net/imagejpeg)($tn,$newname);
}

if($ok==1)
{
return "<img src=".$_FILES['image']['name']." border='0'>";
}
}

if(isset (http://www.php.net/isset)($_POST['Resize']))
{
// imagecrop(Upload Image tmp_path,Upload Image store path,Upload Image type,resize width,resize height);
echo imagecrop($_FILES['image']['tmp_name'],$_FILES['image']['name'],$_FILES['image']['type'],650,250);
}

?>
<form enctype="multipart/form-data" method="post" action="<?PHP $_SERVER['SELF'];?>">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="10%">Upload Image : </td>
<td width="24%">
<input type="file" name="image" /> </td>
<td width="66%"><input type="submit" name="Resize" value="Submit" /></td>
</tr>
</table>
</form>


فقط تنها مشکلی که داره برای ریسایز کردن عدد ثابت بگیره... خو برای عکسها مناسب نیست


echo imagecrop($_FILES['image']['tmp_name'],$_FILES['image']['name'],$_FILES['image']['type'],650,250);

میشه اعدادی که برای ریسایز در انتها قرار داره رو بصورت درصدی نوشت؟!
( البته نا گفته نمونه که نوشتم 50% ام خطا گرفت) راه حلی دارین؟

nokhodi
شنبه 26 مهر 1393, 10:44 صبح
با تغییرات زیر حل شد دوستان :



if(isset($_POST['Resize']))
{
$image_info = getimagesize($_FILES['image']['tmp_name']);
$image_width = $image_info[0];
$image_height = $image_info[1];

$h=$image_height/2;
$w=$image_width/2;

// imagecrop(Upload Image tmp_path,Upload Image store path,Upload Image type,resize width,resize height);
echo imagecrop($_FILES['image']['tmp_name'],$_FILES['image']['name'],$_FILES['image']['type'],$w,$h);
}