تغییر سایز

hodi

Member
سلام دوستان
من یه سیستم ساده نیاز داشتم که بتونم باهاش یک عکس رو در دو سایز آپلود و resize کنم
برای آپلود عکس مشکلی ندارم ولی برای تغییر اندازه نتونستم کاری کنم
کد آپلود رو براتون میزارم (البته کد آپلود رو هم به کمک شما دوستان نوشتم :wink:) اگه ممکنه کدی برای تغییر اندازه عکس بزارید
PHP:
<body style="font-size:12px; font-family:tahoma; color:#777777;" bgcolor="#fff9ec">
<?
    
if($REMOVE = $_GET['REMOVE']){
    unlink($REMOVE);
    $PROMPT = "<b>$REMOVE</b> removed. ";
    $REMOVE="tumb/".$REMOVE;
    unlink($REMOVE);
} elseif ($_FILES["userfile"]['size']){ 
    $upload_dir="./";
    $upload_dir2="tumb/";    
    $userfile = $_FILES["userfile"]['tmp_name']; 
    $userfile_name = $_FILES["userfile"]['name']; 
    $userfile_size = $_FILES["userfile"]['size'];  
    $filename = basename($userfile_name); 
    if(! copy(  $userfile, $upload_dir.$filename  )){ 
        echo "error in upload"; 
        return false; 
    }
    if(! copy(  $userfile, $upload_dir2.$filename  )){
        echo "error in upload"; 
        return false; 
    }
    }
?>
<br>
<form method="post" name="FORM" action="" enctype="multipart/form-data" > 
<input type="file" name="userfile" > 
<input type='submit' value="Upload" style="font-size:12px; height:18px; width:60px; border-width:1px; border-style:groove; border-color:#999999; background-color:#fff5f4;" >
</form> 
<hr align=left style='width:60%; height:1px; color:#dddddd;'>
<br><br>
<?
$dp = opendir('.');
while($dir=readdir($dp)){
    if($dir=='.')continue;
    if($dir=='..')continue;
    if($dir=='access.php')continue;
    if($dir=='error_log')continue;
    if($dir=='index.htm')continue;
    if(is_dir($dir))continue;
    echo " &nbsp; &nbsp; <a href='javascript:if(confirm(\"Are you sure to delete?\"))location.href=\"?REMOVE=$dir\"' style='font-size:14px; font-weight:bold; color:red; text-decoration:none;'  >&nbsp;X&nbsp;</a> 
    <a href='$dir' style='text-decoration:none; color:#a83737'>$dir</a><br>";
    
};
?>
<br><br>
<?=$PROMPT ?>
</body>
با تشکر از همه وستان
 

hodi

Member
ببخشید این کد رو به برنامه اضافه کردم:
PHP:
    header('Content-type: image/jpeg');
    list($width, $height) = getimagesize($filename);
    $new_width =120;
    $new_height = 80;
    $image_p = imagecreatetruecolor($new_width, $new_height);
    $image = imagecreatefromjpeg($filename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    imagejpeg($image_p, null, 100);
ولی موفق نشدم :-?
اگه ممکنه دوستان کمک بیشتری بکنند
 

tarhebartar

Member
بیشتر فکر کن , بایستی فرمولی بنویسی که ارتفاء رو از طریق پهنای جدید بدست بیاره !
 

k2-4u

Well-Known Member
ببخشید این کد رو به برنامه اضافه کردم:
PHP:
    header('Content-type: image/jpeg');
    list($width, $height) = getimagesize($filename);
    $new_width =120;
    $new_height = 80;
    $image_p = imagecreatetruecolor($new_width, $new_height);
    $image = imagecreatefromjpeg($filename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    imagejpeg($image_p, null, 100);
ولی موفق نشدم :-?
اگه ممکنه دوستان کمک بیشتری بکنند

چه . مشکلی داشت
Error داد ؟
 

hodi

Member
بعد اجرای برنامه همچین چیزی نمایش داده میشه
کد:
Øÿà�JFIF������ÿþ�þÝ}3
بزارید کد کامل رو بزارم تا متوجه بشید
PHP:
<body style="font-size:12px; font-family:tahoma; color:#777777;" bgcolor="#fff9ec">
<?
    
if($REMOVE = $_GET['REMOVE']){
    unlink($REMOVE);
    $PROMPT = "<b>$REMOVE</b> removed. ";
    $REMOVE="tumb/".$REMOVE;
    unlink($REMOVE);
} elseif ($_FILES["userfile"]['size']){ 
    $upload_dir="./";
    $upload_dir2="tumb/";    
    $userfile = $_FILES["userfile"]['tmp_name']; 
    $userfile_name = $_FILES["userfile"]['name']; 
    $userfile_size = $_FILES["userfile"]['size'];  
    $filename = basename($userfile_name); 
    if(! copy(  $userfile, $upload_dir.$filename  )){ 
        echo "error in upload"; 
        return false; 
    }
    
    if(! copy(  $userfile, $upload_dir2.$filename  )){
        echo "error in upload"; 
        return false; 
    }
    
    
    list($width, $height) = getimagesize($filename);
    $new_width =120;
    $new_height = 80;
    $image_p = imagecreatetruecolor($new_width, $new_height);
    $image = imagecreatefromjpeg($filename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    imagejpeg($image_p, null, 100);
    
    
    
}
?>
<br>
<form method="post" name="FORM" action="" enctype="multipart/form-data" > 
<input type="file" name="userfile" > 
<input type='submit' value="Upload" style="font-size:12px; height:18px; width:60px; border-width:1px; border-style:groove; border-color:#999999; background-color:#fff5f4;" >
</form> 
<hr align=left style='width:60%; height:1px; color:#dddddd;'>
<br><br>
<?
$dp = opendir('.');
while($dir=readdir($dp)){
    if($dir=='.')continue;
    if($dir=='..')continue;
    if($dir=='access.php')continue;
    if($dir=='error_log')continue;
    if($dir=='index.htm')continue;
    if(is_dir($dir))continue;
    echo " &nbsp; &nbsp; <a href='javascript:if(confirm(\"Are you sure to delete?\"))location.href=\"?REMOVE=$dir\"' style='font-size:14px; font-weight:bold; color:red; text-decoration:none;'  >&nbsp;X&nbsp;</a> 
    <a href='$dir' style='text-decoration:none; color:#a83737'>$dir</a><br>";
    
};
?>
<br><br>
<?=$PROMPT ?>
</body>
 

D.A.V.O.O.D

Member
سلام
اینو تو فایلت اضافه کن درست میشه :

PHP:
@header( 'Content-type: image/jpeg' );
موفق باشی
 

D.A.V.O.O.D

Member
دوباره همین طوری نشون داد ؟
کد:
øÿà�jfif������ÿþ�þý}3
شما می گویید نشد ، ارور می ده که می گویید نمیشه ؟
 

D.A.V.O.O.D

Member
اینو تست کن :

PHP:
<body style="font-size:12px; font-family:tahoma; color:#777777;" bgcolor="#fff9ec">
<?
    
if($REMOVE = $_GET['REMOVE']){
    unlink($REMOVE);
    $PROMPT = "<b>$REMOVE</b> removed. ";
    $REMOVE="tumb/".$REMOVE;
    unlink($REMOVE);
} elseif ($_FILES["userfile"]['size']){ 
    $upload_dir="./";
    $upload_dir2="tumb/";    
    $userfile = $_FILES["userfile"]['tmp_name']; 
    $userfile_name = $_FILES["userfile"]['name']; 
    $userfile_size = $_FILES["userfile"]['size'];  
    $filename = basename($userfile_name); 
    if(! copy(  $userfile, $upload_dir.$filename  )){ 
        echo "error in upload"; 
        return false; 
    }
    
    
    list($width, $height) = getimagesize($filename);
    $new_width =120;
    $new_height = 80;
    $image_p = imagecreatetruecolor($new_width, $new_height);
    $image = imagecreatefromjpeg($filename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    imagejpeg($image_p, $upload_dir2 . $filename , 100);
    
    
    
}
?>
<br>
<form method="post" name="FORM" action="" enctype="multipart/form-data" > 
<input type="file" name="userfile" > 
<input type='submit' value="Upload" style="font-size:12px; height:18px; width:60px; border-width:1px; border-style:groove; border-color:#999999; background-color:#fff5f4;" >
</form> 
<hr align=left style='width:60%; height:1px; color:#dddddd;'>
<br><br>
<?
$dp = opendir('.');
while($dir=readdir($dp)){
    if($dir=='.')continue;
    if($dir=='..')continue;
    if($dir=='access.php')continue;
    if($dir=='error_log')continue;
    if($dir=='index.htm')continue;
    if(is_dir($dir))continue;
    echo " &nbsp; &nbsp; <a href='javascript:if(confirm(\"Are you sure to delete?\"))location.href=\"?REMOVE=$dir\"' style='font-size:14px; font-weight:bold; color:red; text-decoration:none;'  >&nbsp;X&nbsp;</a> 
    <a href='$dir' style='text-decoration:none; color:#a83737'>$dir</a><br>";
    
};
?>
<br><br>
<?=$PROMPT ?>
</body>
 

D.A.V.O.O.D

Member
ممنون از لطفتون:green:
میشه بگید مشکلش از کجا بود؟

خواهش می کنم ، 2 تا اشتباه داشت :
1. شما عکس رو 2 بار آپلود می کردی ، در حالی که اصلا نیازی به این کار نبود ؛ فقط کافی بود که عکس ( در اندازه واقعی ) رو تغییر سایز بدی و اونو تو پوشه tumb ذخیره کنی .

2. آرگومان دوم imagejpeg رو null گذاشته بودی که دقیقا کار این آرگومان سیو کردن فایل بود .

البته ، به هیچ عنوان این کد شما رو تایید نمی کنم ! توی انجمن در مورد آپلود یه سرچ بکن ، این فایل شما بسیار ، بسیار خطرناک است .
 

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

بالا