Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Upload slika kao array

[es] :: PHP :: PHP za početnike :: Upload slika kao array

[ Pregleda: 1382 | Odgovora: 2 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

j4m0r3

Član broj: 146455
Poruke: 95
*.dynamic.isp.telekom.rs.



+19 Profil

icon Upload slika kao array11.04.2013. u 04:06 - pre 133 meseci
Da li neko moze da mi pomogne , kako bi trebalo izmeniti ovu skriptu da bi se na imena slika dodavali $key tj redni brojevi iz niza ?
Npr: 0_321312312321.jpg
1_5464563421.jpg
2_4789023773.jpg
samo sto vec ovde postoji rename sa random brojevima , ali trebalo na to da se dodaje

Code:

<?php
//If you face any errors, increase values of "post_max_size", "upload_max_filesize" and "memory_limit" as required in php.ini
include "konekcija.php";

$sql="INSERT INTO predmet (korisnik_id,ktg_id,grupa_id,naziv,vrednost,cena,preuzimanje,stanje,garantni_list,opis,zamena_za,grad)
VALUES
('$korisnik_id','$_POST[ktg_id]','$_POST[grupa_id]','$_POST[naziv]','$_POST[vrednost]','$_POST[cena]','$_POST[preuzimanje]','$_POST[stanje]','$_POST[garantni_list]','$_POST[opis]','$_POST[zamena_za]','$_POST[grad]')";

if (!mysql_query($sql,$konekcija))
  {
  die('Error: ' . mysql_error());
  }

 //Some Settings
$ThumbSquareSize         = 200; //Thumbnail will be 200x200
$BigImageMaxSize         = 800; //Image Maximum height or width
$ThumbPrefix            = "thumb_"; //Normal thumb Prefix
$DestinationDirectory    = 'uploads/'; //Upload Directory ends with / (slash)
$Quality                 = 90;

//ini_set('memory_limit', '-1'); // maximum memory!

foreach($_FILES as $file )
{
// some information about image we need later.
$ImageName         = $file.$file['name'];
$ImageSize         = $file['size'];
$TempSrc         = $file['tmp_name'];
$ImageType         = $file['type'];




if (is_array($ImageName)) 
{
    $c = count($ImageName);
    
    echo  '<ul>';
    
    for ($i=0; $i < $c; $i++)
    {
        $processImage            = true;    
        $RandomNumber            = rand(0, 9999999999);  // We need same random name for both files.
        
        if(!isset($ImageName[$i]) || !is_uploaded_file($TempSrc[$i]))
        {
            echo '<div class="error">Greska pri postavljanju <strong>'.$ImageName[$i].'</strong>,prevelik fajl!</div>'; //output error
        }
        else
        {
            //Validate file + create image from uploaded file.
            switch(strtolower($ImageType[$i]))
            {
                case 'image/png':
                    $CreatedImage = imagecreatefrompng($TempSrc[$i]);
                    break;
                case 'image/gif':
                    $CreatedImage = imagecreatefromgif($TempSrc[$i]);
                    break;
                case 'image/jpeg':
                case 'image/pjpeg':
                    $CreatedImage = imagecreatefromjpeg($TempSrc[$i]);
                    break;
                default:
                    $processImage = false; //image format is not supported!
            }
            //get Image Size
            list($CurWidth,$CurHeight)=getimagesize($TempSrc[$i]);

            //Get file extension from Image name, this will be re-added after random name
            $ImageExt = substr($ImageName[$i], strrpos($ImageName[$i], '.'));
            $ImageExt = str_replace('.','',$ImageExt);
    
            //Construct a new image name (with random number added) for our new image.
            $NewImageName = $RandomNumber.'.'.$ImageExt;

            //Set the Destination Image path with Random Name
            $thumb_DestRandImageName     = $DestinationDirectory.$ThumbPrefix.$NewImageName; //Thumb name
            $DestRandImageName             = $DestinationDirectory.$NewImageName; //Name for Big Image

            //Resize image to our Specified Size by calling resizeImage function.
            if($processImage && resizeImage($CurWidth,$CurHeight,$BigImageMaxSize,$DestRandImageName,$CreatedImage,$Quality,$ImageType[$i]))
            {
                //Create a square Thumbnail right after, this time we are using cropImage() function
                if(!cropImage($CurWidth,$CurHeight,$ThumbSquareSize,$thumb_DestRandImageName,$CreatedImage,$Quality,$ImageType[$i]))
                    {
                        echo 'Error Creating thumbnail';
                    }
                    /*
                    At this point we have succesfully resized and created thumbnail image
                    We can render image to user's browser or store information in the database
                    For demo, we are going to output results on browser.
                    */
                    
                    //Get New Image Size
                    list($ResizedWidth,$ResizedHeight)=getimagesize($DestRandImageName);
                    
                    
                    
                    //echo '<img src="uploads/'.$ThumbPrefix.$NewImageName.'" alt="Thumbnail" height="'.$ThumbSquareSize.'" width="'.$ThumbSquareSize.'">';
                    //echo '</tr><tr>';
                    //echo '<td align="center"><img src="uploads/'.$NewImageName.'" alt="Resized Image" height="'.$ResizedHeight.'" width="'.$ResizedWidth.'"></td>';
                    
                    
                    // Insert info into database table!
                    mysql_query("INSERT INTO slike (predmet_id, korisnik_id, mala, velika)
                    VALUES (1, 1, '$thumb_DestRandImageName', '$DestRandImageName')");
                    

                    

            }else{
                echo '<div class="error">Greska pri postavljanju  <strong>'.$ImageName[$i].'</strong>! Proverite ekstenziju </div>'; //output error
            }
            
        }
        
    }
    echo '</ul>';
    
    }
}
    
// This function will proportionally resize image 
function resizeImage($CurWidth,$CurHeight,$MaxSize,$DestFolder,$SrcImage,$Quality,$ImageType)
{
    //Check Image size is not 0
    if($CurWidth <= 0 || $CurHeight <= 0) 
    {
        return false;
    }
    
    //Construct a proportional size of new image
    $ImageScale          = min($MaxSize/$CurWidth, $MaxSize/$CurHeight); 
    $NewWidth              = ceil($ImageScale*$CurWidth);
    $NewHeight             = ceil($ImageScale*$CurHeight);
    
    if($CurWidth < $NewWidth || $CurHeight < $NewHeight)
    {
        $NewWidth = $CurWidth;
        $NewHeight = $CurHeight;
    }
    $NewCanves     = imagecreatetruecolor($NewWidth, $NewHeight);
    // Resize Image
    if(imagecopyresampled($NewCanves, $SrcImage,0, 0, 0, 0, $NewWidth, $NewHeight, $CurWidth, $CurHeight))
    {
        switch(strtolower($ImageType))
        {
            case 'image/png':
                imagepng($NewCanves,$DestFolder);
                break;
            case 'image/gif':
                imagegif($NewCanves,$DestFolder);
                break;            
            case 'image/jpeg':
            case 'image/pjpeg':
                imagejpeg($NewCanves,$DestFolder,$Quality);
                break;
            default:
                return false;
        }
    if(is_resource($NewCanves)) { 
      imagedestroy($NewCanves); 
    } 
    return true;
    }

}

//This function corps image to create exact square images, no matter what its original size!
function cropImage($CurWidth,$CurHeight,$iSize,$DestFolder,$SrcImage,$Quality,$ImageType)
{     
    //Check Image size is not 0
    if($CurWidth <= 0 || $CurHeight <= 0) 
    {
        return false;
    }
    
    //abeautifulsite.net has excellent article about "Cropping an Image to Make Square"
    //http://www.abeautifulsite.net/blog/2009/08/cropping-an-image-to-make-square-thumbnails-in-php/
    if($CurWidth>$CurHeight)
    {
        $y_offset = 0;
        $x_offset = ($CurWidth - $CurHeight) / 2;
        $square_size     = $CurWidth - ($x_offset * 2);
    }else{
        $x_offset = 0;
        $y_offset = ($CurHeight - $CurWidth) / 2;
        $square_size = $CurHeight - ($y_offset * 2);
    }
    
    $NewCanves     = imagecreatetruecolor($iSize, $iSize);    
    if(imagecopyresampled($NewCanves, $SrcImage,0, 0, $x_offset, $y_offset, $iSize, $iSize, $square_size, $square_size))
    {
        switch(strtolower($ImageType))
        {
            case 'image/png':
                imagepng($NewCanves,$DestFolder);
                break;
            case 'image/gif':
                imagegif($NewCanves,$DestFolder);
                break;            
            case 'image/jpeg':
            case 'image/pjpeg':
                imagejpeg($NewCanves,$DestFolder,$Quality);
                break;
            default:
                return false;
        }
    if(is_resource($NewCanves)) { 
      imagedestroy($NewCanves); 
    } 
    return true;

    }
      
}



[Ovu poruku je menjao j4m0r3 dana 11.04.2013. u 08:07 GMT+1]
 
Odgovor na temu

ivan.a
PHP developer

Član broj: 83976
Poruke: 403
89.216.28.*



+44 Profil

icon Re: Upload slika kao array11.04.2013. u 10:15 - pre 133 meseci
Ovaj kod:
Code:
$NewImageName = $RandomNumber.'.'.$ImageExt;

zameni sa:
Code:
$NewImageName = $i."_".$RandomNumber.'.'.$ImageExt;


Takodje, ovo:
Code:
$ThumbPrefix            = "thumb_"; //Normal thumb Prefix

zameni sa:
Code:
$ThumbPrefix            = ""; //Normal thumb Prefix

I hope I didn't brain my damage - Homer
if (wife.position == kitchen) {return sandwich};
 
Odgovor na temu

j4m0r3

Član broj: 146455
Poruke: 95
*.dynamic.isp.telekom.rs.



+19 Profil

icon Re: Upload slika kao array11.04.2013. u 11:20 - pre 133 meseci
Svaka cast Ivane, genije si.... Imas pivo javi se !!
 
Odgovor na temu

[es] :: PHP :: PHP za početnike :: Upload slika kao array

[ Pregleda: 1382 | Odgovora: 2 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.