IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

EDI, CMS, Outils, Scripts et API PHP Discussion :

Redimensionner image pendant upload


Sujet :

EDI, CMS, Outils, Scripts et API PHP

  1. #1
    Membre du Club
    Homme Profil pro
    Inscrit en
    Mars 2007
    Messages
    95
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : Belgique

    Informations professionnelles :
    Secteur : Services de proximité

    Informations forums :
    Inscription : Mars 2007
    Messages : 95
    Points : 48
    Points
    48
    Par défaut Redimensionner image pendant upload
    Bonjour,

    Je cherche depuis très longtemps à pouvoir redimensionner les images pendant l'upload avant de les sauvegarder sur le serveur.

    En effet, les photos envoyées par les membres sont trop grandes et je voudrais les redimensionner en taille maximum : 640x480

    Comment faire ?

    Voici mon code savesnap.php

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
     
    <?php
     
    if ( !defined( 'SMARTY_DIR' ) ) {
    	include_once( 'init.php' );
    }
     
    include ( 'sessioninc.php' );
     
    $userid = $_SESSION['UserId'];
     
    $sql = 'SELECT id, picture, tnpicture FROM ! WHERE userid = ? AND picno = ?';
     
    $row = $db->getRow( $sql, array( USER_SNAP_TABLE, $userid, $_POST['txtpicno'] ) );
     
    $userinfo = $db->getRow('select * from ! where id = ?', array( USER_TABLE, $userid) );
     
    $err = 0;
     
    if ($config['snaps_require_approval'] == 'Y') {
     
    	$act = 'N';
     
    } else {
     
    	$act = 'Y';
     
    }
     
    $curr_imgfile = $curr_tnimgfile = '';
     
    if (substr_count($row['picture'], 'file:' )>0 ) {
    	$curr_imgfile = ltrim(rtrim(str_replace('file:','',$row['picture'] ) ) );
    }
    if (substr_count($row['tnpicture'],'file:' )>0 ) {
    	$curr_tnimgfile = ltrim(rtrim(str_replace('file:','',$row['tnpicture'] ) ) );
    }
     
    $allwdsize = $config['upload_snap_maxsize'];
     
    if ($_POST['album_name'] != '') {
     
    /* Add new album first and then process the image */
     
    	$album_id = $db->getOne('select id from ! where name = ? and username = ?', array(USERALBUMS_TABLE, $_POST['album_name'], $userinfo['username'] )  );
     
    	if ($album_id > 0 ) {
    		null;
    	} else {
    		$db->query('insert into ! (username, name, passwd) values (?, ?, ?)', array( USERALBUMS_TABLE, $userinfo['username'], $_POST['album_name'], md5($_POST['album_passwd'])) );
     
    		$album_id = $db->getOne('select id from ! where name = ? and username = ? ', array(USERALBUMS_TABLE, $_POST['album_name'], $userinfo['username']  )  );
    	}
     
    } else {
     
    	$album_id = $_POST['album_id'];
     
    }
     
    if (isset($_POST['changealbum']) ) {
    /* Change album name  */
     
    	$sql = "update ! set album_id = ? where userid = ? and picno = ?";
    	$db->query($sql, array(USER_SNAP_TABLE, $album_id, $userid, $_POST['txtpicno']) );
    	header( 'location: uploadsnaps.php?msg='.ALBUM_CHANGED );
    	exit;
     
    }
     
    if (isset($_POST['changedescription']) ) { 
    /* Change description  */ 
     
        $sql = "update ! set description = ? where userid = ? and picno = ?"; 
        $db->query($sql, array(USER_SNAP_TABLE, strip_tags($_POST['description']), $userid, $_POST['txtpicno']) ); 
        header( 'location: uploadsnaps.php?msg='.DESCRIPTION_CHANGED ); 
        exit; 
     
    }    
     
    if( is_uploaded_file( $_FILES['txtimage']['tmp_name'] ) ) {
     
    	$img_file = $_FILES['txtimage']['tmp_name'];
     
    	$ext = split( '/', $_FILES['txtimage']['type'] );
     
    	$picext = strtolower($ext[1]);
     
    	if( $picext == 'pjpeg' || $picext == 'jpeg'){
     
    		$picext = 'jpg';
    	}
     
    	if( $picext == 'x-png' ) {
    		$picext= 'png';
    	}
    	//echo "$picext<br>";
     
    	$ext_ok = '0';
     
    	foreach (explode(',',$config['upload_snap_ext']) as $ex) {
     
     
    		if ( $ex == $picext ) $ext_ok++;
     
    	}
     
    /* bmp is removed as valid source time being */
    	if ( $ext_ok <= '0' or $picext == 'bmp') {
     
    		header( 'location: uploadsnaps.php?msg=' .WRONG_TYPE  );
    		exit;
     
    	}
     
    	clearstatcache();
     
    	$fstats= stat($img_file);
     
    	$picsize = $fstats[7];
     
    	$handle = fopen ($img_file, 'rb');
     
    	/* Get current picture size and allowed size. If pic size is more than the allowed size, flag error.. */
     
     
    	if ($picsize > $allwdsize) {
     
    		header( 'location: uploadsnaps.php?msg='.BIG_PIC_SIZE );
    		exit;
     
    	}
     
    	$orgimg = fread($handle, $picsize);
     
    	fclose ($handle);
     
    	if ($_POST['txtpicno'] == '' or !isset($_POST['txtpicno']) ) {
    		$_POST['txtpicno'] = $db->getOne('select max(picno) from ! where userid = ?',array(USER_SNAP_TABLE,$userid) )+1;
    	}
     
    	if ( $picext != 'jpg' ) {
    	/* convert the picture to jpg. This is to enable picture editing  */
     
     
    		//$jpgfile = createThumb($orgimg, 'N');
    		$img_tmp=createImg($picext,$img_file);
    		$jpgfile = createJpeg($img_tmp, 'N');
    		$newimg = file_get_contents($jpgfile);
     
    	} else {
     
    		$newimg = $orgimg;
    	}
     
    	$img_tmp=createImg($picext,$img_file);
     
    	$tnimg_file = createJpeg($img_tmp,'Y');
     
    	$tnimg = file_get_contents($tnimg_file);
     
    	$tnext = 'jpg';
     
    	$picext = 'jpg';
     
    	if ($config['images_in_db'] == 'N') {
     
    		$imgfile = writeImageToFile($newimg, $userid, '1'.$_POST['txtpicno'],$curr_imgfile);
     
    		$newimg = 'file:'.$imgfile;
    		sleep(5);
     
    		$tnimgfile = writeImageToFile($tnimg, $userid, '2'.$_POST['txtpicno'],$curr_tnimgfile);
     
    		$tnimg = 'file:'.$tnimgfile;
     
    	} else {
     
    		$newimg = base64_encode($newimg);
     
    		$tnimg = base64_encode($tnimg);
    	}
     
    if ( $row ) {
     
            $sql = 'update ! set picture = ?, ins_time = ?, active=?, picext=?, tnpicture = ?, tnext = ?, album_id = ?, description = ?  where userid = ? and picno = ? and id = ?';
     
            $db->query( $sql, array( USER_SNAP_TABLE, $newimg, $time, $act,    $picext, $tnimg, $tnext, $album_id, $userid, $_POST['txtpicno'], $row['id'],  strip_tags($_POST['description']) ) );
     
        } else {
     
            if ($_POST['txtpicno'] == '' or !isset($_POST['txtpicno']) ) {
                $_POST['txtpicno'] = $db->getOne('select max(picno) from ! where userid = ?',array(USER_SNAP_TABLE,$userid) )+1;
            }
            $sql = 'insert into ! (  userid, picno, picture, ins_time, active, picext, tnpicture, tnext, album_id, description ) values (  ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )';
     
            $db->query( $sql, array( USER_SNAP_TABLE, $userid, $_POST['txtpicno'], $newimg, $time, $act, $picext, $tnimg, $tnext, $album_id,  strip_tags($_POST['description']) ) ); 
     
    	}
    	header( 'location: uploadsnaps.php?msg='.PICTURE_LOADED );
    	exit;
     
    }
     
    if ( is_uploaded_file( $_FILES['tnimage']['tmp_name'] ) ) {
     
    	$tnimg_file = $_FILES['tnimage']['tmp_name'];
     
    	$ext = split( '/', $_FILES['tnimage']['type'] );
     
    	$tnext = strtolower($ext[1]);
     
    	$tnsize = $config['upload_snap_tnsize'];
     
    	if( $tnext == 'pjpeg' || $tnext == 'jpeg'){
     
    		$tnext = 'jpg';
     
    	}
     
    	if( $tnext == 'x-png' ) {
    		$tnext= 'png';
    	}
     
    	$ext_ok = 0;
     
    	foreach (explode(',',$config['upload_snap_ext']) as $ex) {
     
    		if ( $ex == $tnext ) $ext_ok++;
     
    	}
     
    	if ( $ext_ok <= 0 ) {
     
    		header( 'location: uploadsnaps.php?msg=' .WRONG_TYPE  );
    		exit;
     
    	}
     
    	clearstatcache();
     
    	$fstats= stat($tnimg_file);
     
    	$picsize = $fstats[7];
     
    	if ($picsize > $allwdsize) {
     
    		header( 'location: uploadsnaps.php?msg='.BIG_PIC_SIZE );
    		exit;
     
    	}
     
    	list($tnwidth, $tnheight, $tntype, $tnattr) = getimagesize($tnimg_file);
     
     
    	if ($tnwidth > $tnsize or $tnheight > $tnsize) {
     
    			header( 'location: uploadsnaps.php?msg='.BIGTHUMBNAIL );
    			exit;
    	}
     
    	$handle = fopen ($tnimg_file, 'rb');
     
    	/* Get current picture size and allowed size. If pic size is more than the allowed size, flag error.. */
     
    	$tnimg = fread($handle, filesize ($tnimg_file));
     
    	fclose ($handle);
     
    	if ( $tnext != 'jpg' ) {
     
    	/* convert the picture to jpg. This is to enable picture editing  */
     
    		//$jpgfile = createThumb($tnimg, 'N');
    		$img_tmp=createImg($tnext,$tnimg_file);
    		$jpgfile = createJpeg($img_tmp, 'N');
     
    		$newtnimg = file_get_contents($jpgfile);
     
    		unlink($jpgfile);
     
    		$tnext = 'jpg';
     
    	} else {
     
    		$newtnimg = $tnimg;
    	}
     
     
    	$tnimg = base64_encode( $newtnimg );
     
    	if ($config['images_in_db'] == 'N') {
     
    		$tnimgfile = writeImageToFile($tnimg, $userid, $_POST['txtpicno'], $curr_tnimgfile);
     
    		$tnimg = 'file:'.tnimgfile;
    	}
     
    	if ($row) {
     
    		$sql = 'update ! set tnpicture = ?, ins_time = ?, active=?, tnext=?, album_id = ? where id = ?';
     
    		$db->query( $sql, array( USER_SNAP_TABLE, $tnimg, $time, $act, 	$tnext, $album_id, $row['id'] ) );
     
    	} else {
     
    		$sql = 'insert into ! (  userid, picno, tnpicture, ins_time, active, tnext, album_id ) values (  ?, ?, ?, ?, ?, ?, ? )';
     
    		$db->query( $sql, array( USER_SNAP_TABLE,  $userid, $_POST['txtpicno'], $tnimg, $time, $act, $tnext, $album_id ) );
     
    	}
     
    	updateLoadedPicturesCnt($userid);
     
    	if ($config['newpic_admin_info'] == 'Y') {
    		sendAdminEmail();
    	}
     
    	header( 'location: uploadsnaps.php?msg='.PICTURE_LOADED );
    	exit;
     
    }
     
    header( 'location: uploadsnaps.php?msg='.FAILED_UPLOAD );
    exit;
     
    function createImg($type,$file) {
    	$type=strtolower($type);
    	if($type == 'bmp') $img=imagecreatefromwbmp($file);
    	else if($type == 'png') $img=imagecreatefrompng($file);
    	else if($type == 'gif') $img=imagecreatefromgif($file);
    	else if($type == 'jpg') $img=imagecreatefromjpeg($file);
    	return $img;
    }
     
     
     
    function createJpeg( $img , $reduce='Y') {
     
    	global $config;
    	global $userid;
    	global $ext;
     
    	$tnsize = $config['upload_snap_tnsize'];
     
    	//$img = imagecreatefrompng($org);
     
    	$w = imagesx( $img );
     
    	$h = imagesy( $img );
     
    	if ($reduce == 'Y' && ($w > $tnsize || $h > $tnsize)) {
    		if( $w > $h ) {
    			$ratio = $w / $h;
    			$nw = $tnsize;
    			$nh = $nw / $ratio;
    		} else {
    			$ratio = $h / $w;
    			$nh = $tnsize;
    			$nw = $nh /$ratio;
    		}
    	} else {
     
    		$nh = $h;
    		$nw = $w;
    	}
     
    	$img2 = imagecreatetruecolor( $nw, $nh );
     
    	imagecopyresampled ( $img2, $img, 0, 0, 0 , 0, $nw, $nh, $w, $h );
     
    	$fimg = 'img_' . time().$userid . '.jpg';
     
    	$real_tpath = realpath ("temp");
     
    	if(	$HTTP_ENV_VARS['OS'] == 'Windows_NT'){
     
    		$real_tpath= str_replace( "\\", "\\\\", $real_tpath);
     
    		$file = $real_tpath . "\\" . $fimg;
     
    	}else{
     
    		$file = $real_tpath . "/" . $fimg;
     
    	}
     
    	imagejpeg( $img2, $file );
     
    	imagedestroy($img2);
     
    	imagedestroy($img);
     
    	return $file;
    }
     
     
     
    function writeImageToFile($img, $userid, $picno, $file="") {
    /* This routine will create an image file */
    	if ($file == '') {
    		$filename= time().$userid.$picno.'.jpg';
    	} else {
    		$filename = $file;
    	}
     
    	$img = imagecreatefromstring( $img );
    	imagejpeg($img, USER_IMAGE_DIR.$filename);
     
    	return ($filename);
    }
     
    ?>

  2. #2
    Membre averti Avatar de Huntress
    Femme Profil pro
    Inscrit en
    Août 2004
    Messages
    475
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations forums :
    Inscription : Août 2004
    Messages : 475
    Points : 402
    Points
    402
    Par défaut
    Bonsoir,

    Euh je comprends pas... tu te sers déjà de la fonction imagecopyresampled, que veux-tu faire de plus ? Le tout est de resizer l'image que tu viens d'uploader... comme çà exit la photo qui fait 8000 par 5000... nan ?

    Règle n°1 du forum francophone : Parler le français...
    Pas de question technique par MP, le forum est fait pour çà, merci.

  3. #3
    Membre du Club
    Homme Profil pro
    Inscrit en
    Mars 2007
    Messages
    95
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : Belgique

    Informations professionnelles :
    Secteur : Services de proximité

    Informations forums :
    Inscription : Mars 2007
    Messages : 95
    Points : 48
    Points
    48
    Par défaut
    Oui mais c'est pas moi qui l'ai fait ce code donc je sais pas quoi modifier pour que la photo soit envoyée sur le serveur avec une taille max de 640x480

  4. #4
    Membre averti
    Profil pro
    Inscrit en
    Janvier 2004
    Messages
    489
    Détails du profil
    Informations personnelles :
    Âge : 51
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Janvier 2004
    Messages : 489
    Points : 388
    Points
    388
    Par défaut
    c'est plutot une fois que l'image a ete uploadée qu'il faut la resizer.. avant, tu ne peux rien faire.. et tu ne connais meme pas ses dimensions, avant l'upload

  5. #5
    Membre du Club
    Homme Profil pro
    Inscrit en
    Mars 2007
    Messages
    95
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : Belgique

    Informations professionnelles :
    Secteur : Services de proximité

    Informations forums :
    Inscription : Mars 2007
    Messages : 95
    Points : 48
    Points
    48
    Par défaut
    J'ai essayé de modifier mon code grace à l'exemple 2 de
    http://php.net/manual/en/function.im...yresampled.php

    Mais toujours pas de redimensionnement, l'image m'apparait sur le site dans sa taille d'origine.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
     
    	//$img = imagecreatefrompng($org);
    //DEBUT MOD
    	$w = 500;
     
    	$h = 400;
     
    	list($width_orig, $height_orig) = getimagesize($img);
     
    	$ratio_orig = $width_orig/$height_orig;
     
    	if ($w/$h > $ratio_orig) {
       		$w = $h*$ratio_orig;
    	} else {
    		$h = $w/$ratio_orig;
    	}
     
    // Redimensionnement
    	$img2 = imagecreatetruecolor($w, $h);
    	imagecopyresampled($img2, $img, 0, 0, 0, 0, $w, $h, $width_orig, $height_orig);
    //FIN MOD

Discussions similaires

  1. Redimensionnement images en upload coté serveur
    Par nessag dans le forum Langage
    Réponses: 5
    Dernier message: 26/04/2015, 01h06
  2. Redimensionnement image après upload
    Par thomix68 dans le forum Langage
    Réponses: 7
    Dernier message: 22/10/2010, 16h36
  3. Redimensionnement image et upload
    Par toufou dans le forum jQuery
    Réponses: 8
    Dernier message: 08/07/2010, 14h56
  4. [Upload] Redimensionner image avant upload
    Par spawns dans le forum Langage
    Réponses: 3
    Dernier message: 08/05/2009, 10h16
  5. [Upload] Redimensionner image après upload
    Par leroidje dans le forum Langage
    Réponses: 3
    Dernier message: 21/11/2008, 11h12

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo