i don't know how execute multi requete sql
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_SESSION['pseudo']) && !empty($_SESSION['pseudo']) && isset($_SESSION['user_id']) && !empty($_SESSION['user_id'])) {
$id = trim(htmlspecialchars($_SESSION['user_id']));
$pseudo = trim(htmlspecialchars($_SESSION['pseudo']));
$stmt->$connect->prepare('SELECT id,pseudo,jeton FROM users WHERE id=:id AND pseudo=:pseudo');
$stmt->execute(array(
':id' => $id,
':pseudo' => $pseudo
));
$row = $stmt->fetch();
if ($_SESSION['pseudo'] == $row['pseudo'] && $_SESSION['user_id'] == $row['id']) {
// Files Image profil
$avatarName = $_FILES['avatarCouverture']['name'];
$avatarTmp = $_FILES['avatarCouverture']['tmp_name'];
$avatarSize = $_FILES['avatarCouverture']['size'];
$avatarType = $_FILES['avatarCouverture']['type'];
$extension = array(
'jpg',
'jpeg',
'png'
);
$avatarExplode = explode('.', $avatarName);
$avatarEnd = end($avatarExplode);
$avatarLower = strtolower($avatarEnd);
$saveIdUser = trim(htmlspecialchars($_SESSION['user_id']));
$saveIdUserNew = filter_var($saveIdUser, FILTER_SANITIZE_NUMBER_INT);
$avatar = rand(0, 100000) . '_' . date('Y-m-d H-i-s') . '_' . $saveIdUserNew;
if (isset($avatarName) && !empty($avatarName)) {
if ($avatarSize < 8000000) {
if (in_array($avatarLower, $extension)) {
if (isset($avatarTmp) && !empty($avatarTmp)) {
$image = getimagesize($avatarTmp);
if ($image['mime'] === 'image/jpeg') {
$imageSrc = imagecreatefromjpeg($avatarTmp);
} elseif ($image['mime'] === 'image/png') {
$imageSrc = imagecreatefrompng($avatarTmp);
} else {
$imageSrc = false;
}
// modifier size image
if ($imageSrc !== false) {
$imageWidth = 60;
$imageHeight = 60;
if ($image[0] >= $imageWidth && $image[1] >= $imageHeight) {
$imageFinale = $imageSrc;
} else {
$newWidth[0] = $imageWidth;
$newHeight[1] = $imageHeight;
$imageFinale = imagecreatetruecolor($newWidth[0], $newHeight[1]);
imagecopyresampled($imageFinale, $imageSrc, 0, 0, 0, 0, $newWidth[0], $newHeight[1], $image[0], $image[1]);
}
$target = imagejpeg($imageFinale, 'upload/avatarPost/' . $avatar . '.jpg');
move_uploaded_file($avatarTmp, $target);
} // ! == false
}
}
}
}
$stmt = $connect->prepare('INSERT INTO users(avatarcouverture) VALUES(:avatarne) ');
$stmt->execute(array(
":avatarne" => $avatar
));
//header('Location: profil.php');
}
}
}
?>
I think the issue was the initial prepared statement assignment
$stmt->$connect->prepare('SELECT id,pseudo,jeton FROM users WHERE id=:id AND pseudo=:pseudo');
it should be perhaps more like this.
$stmt = $connect->prepare('SELECT id, pseudo, jeton FROM users WHERE id=:id AND pseudo=:pseudo');
The final sql query surely should only be run if there is a successfully added avatar? Reorganised a little whilst looking for issues - hope it helps.
if( $_SERVER['REQUEST_METHOD'] === 'POST' ) {
if( !empty( $_SESSION['pseudo'] ) && !empty( $_SESSION['user_id'] ) ) {
$id = trim( htmlspecialchars( $_SESSION['user_id'] ) );
$pseudo = trim( htmlspecialchars($_SESSION['pseudo'] ) );
/*
Assign the prepared statement as a variable
*/
$stmt = $connect->prepare('SELECT id, pseudo, jeton FROM users WHERE id=:id AND pseudo=:pseudo');
if( !$stmt )exit('Failed to prepare sql');
$stmt->execute( array(
':id' => $id,
':pseudo' => $pseudo
)
);
$row=$stmt->fetch();
if( $_SESSION['pseudo'] == $row['pseudo'] && $_SESSION['user_id'] == $row['id'] ) {
$avatarName = $_FILES['avatarCouverture']['name'];
$avatarTmp = $_FILES['avatarCouverture']['tmp_name'];
$avatarSize = $_FILES['avatarCouverture']['size'];
$avatarType = $_FILES['avatarCouverture']['type'];
$extension = array('jpg','jpeg','png');
$avatarLower = strtolower( pathinfo($avatarName,PATHINFO_EXTENSION ) );
$saveIdUser = trim( htmlspecialchars( $_SESSION['user_id'] ) );
$saveIdUserNew = filter_var( $saveIdUser, FILTER_SANITIZE_NUMBER_INT );
$avatar = rand(0,100000).'_'.date('Y-m-d H-i-s').'_'.$saveIdUserNew;
if( !empty( $avatarName ) ) {
if( $avatarSize < 8000000 ) {
if( in_array( $avatarLower, $extension ) ){
if( !empty( $avatarTmp ) ) {
$image = getimagesize( $avatarTmp );
if( $image['mime'] === 'image/jpeg' ) {
$imageSrc = imagecreatefromjpeg( $avatarTmp );
} elseif( $image['mime']==='image/png' ) {
$imageSrc = imagecreatefrompng( $avatarTmp );
} else {
$imageSrc = false;
}
// modifier size image
if( $imageSrc !== false ) {
$imageWidth = 60;
$imageHeight = 60;
if( $image[0]>= $imageWidth && $image[1]>= $imageHeight ) {
$imageFinale = $imageSrc ;
} else {
$newWidth[0] = $imageWidth;
$newHeight[1] = $imageHeight;
$imageFinale = imagecreatetruecolor( $newWidth[0], $newHeight[1] );
imagecopyresampled( $imageFinale, $imageSrc, 0,0,0,0, $newWidth[0], $newHeight[1], $image[0], $image[1] );
}
$target = imagejpeg( $imageFinale,'upload/avatarPost/'.$avatar.'.jpg' );
move_uploaded_file( $avatarTmp ,$target );
$stmt=$connect->prepare('INSERT INTO users( avatarcouverture ) VALUES( :avatarne )');
if( !$stmt )exit('Failed to prepare sql');
$stmt->execute( array( ":avatarne"=> $avatar ) );
}
}
}
}
}
//header('Location: profil.php');
}
}
}
Related
Getting the above warning for the following line of code:
switch ( $imagetype[2] ) {
The rest of the code for this below:
function nzshpcrt_display_preview_image() {
global $wpdb;
if ( (isset( $_GET['wpsc_request_image'] ) && ($_GET['wpsc_request_image'] == 'true'))
|| (isset( $_GET['productid'] ) && is_numeric( $_GET['productid'] ))
|| (isset( $_GET['image_id'] ) && is_numeric( $_GET['image_id'] ))
|| (isset( $_GET['image_name'] ))
) {
if ( function_exists( "getimagesize" ) ) {
$imagepath = '';
$category_id = 0;
if(isset($_GET['image_name'] )) {
$image = basename( $_GET['image_name'] );
$imagepath = WPSC_USER_UPLOADS_DIR . $image;
} else if ( isset($_GET['category_id'] )) {
$category_id = absint( $_GET['category_id'] );
$image = $wpdb->get_var( $wpdb->prepare( "SELECT `image` FROM `" . WPSC_TABLE_PRODUCT_CATEGORIES . "` WHERE `id` = %d LIMIT 1", $category_id ) );
if ( $image != '' ) {
$imagepath = WPSC_CATEGORY_DIR . $image;
}
}
if ( ! is_file( $imagepath ) ) {
$imagepath = WPSC_FILE_PATH . "/images/no-image-uploaded.gif";
}
$image_size = #getimagesize( $imagepath );
if ( is_numeric( $_GET['height'] ) && is_numeric( $_GET['width'] ) ) {
$height = (int)$_GET['height'];
$width = (int)$_GET['width'];
} else {
$width = $image_size[0];
$height = $image_size[1];
}
if ( !(($height > 0) && ($height <= 1024) && ($width > 0) && ($width <= 1024)) ) {
$width = $image_size[0];
$height = $image_size[1];
}
$product_id = (int) $_GET ??'' ['productid'];
$image_id = (int) $_GET ??'' ['image_id'];
if ( $product_id > 0 ) {
$cache_filename = basename( "product_{$product_id}_{$height}x{$width}" );
} else if ( $category_id > 0 ) {
$cache_filename = basename( "category_{$category_id}_{$height}x{$width}" );
} else {
$cache_filename = basename( "product_img_{$image_id}_{$height}x{$width}" );
}
$imagetype = #getimagesize( $imagepath );
$use_cache = false;
switch ( $imagetype[2]?? '') {
case IMAGETYPE_GIF:
$extension = ".gif";
break;
case IMAGETYPE_PNG:
$extension = ".png";
break;
case IMAGETYPE_JPEG:
default:
$extension = ".jpg";
break;
}
if ( file_exists( WPSC_CACHE_DIR . $cache_filename . $extension ) ) {
$original_modification_time = filemtime( $imagepath );
$cache_modification_time = filemtime( WPSC_CACHE_DIR . $cache_filename . $extension );
if ( $original_modification_time < $cache_modification_time ) {
$use_cache = true;
}
}
if ( $use_cache === true ) {
$cache_url = set_url_scheme( WPSC_CACHE_URL );
header( "Location: " . $cache_url . $cache_filename . $extension );
exit( '' );
} else {
switch ( $imagetype[2] ) {
case IMAGETYPE_JPEG:
$src_img = imagecreatefromjpeg( $imagepath );
$pass_imgtype = true;
break;
case IMAGETYPE_GIF:
$src_img = imagecreatefromgif( $imagepath );
$pass_imgtype = true;
break;
case IMAGETYPE_PNG:
$src_img = imagecreatefrompng( $imagepath );
$pass_imgtype = true;
break;
default:
$src_img = false;
$pass_imgtype = false;
break;
}
if ( $pass_imgtype === true && $src_img ) {
$source_w = imagesx( $src_img );
$source_h = imagesy( $src_img );
//Temp dimensions to crop image properly
$temp_w = $width;
$temp_h = $height;
// select our scaling method
$scaling_method = apply_filters( 'wpsc_preview_image_cropping_method', 'cropping' );
// set both offsets to zero
$offset_x = $offset_y = 0;
// Here are the scaling methods, non-cropping causes black lines in tall images, but doesnt crop images.
switch ( $scaling_method ) {
case 'cropping':
// if the image is wider than it is high and at least as wide as the target width.
if ( ($source_h <= $source_w ) ) {
if ( $height < $width ) {
$temp_h = ($width / $source_w) * $source_h;
} else {
$temp_w = ($height / $source_h) * $source_w;
}
} else {
$temp_h = ($width / $source_w) * $source_h;
}
break;
case 'non-cropping':
default:
if ( $height < $width ) {
$temp_h = ($width / $source_w) * $source_h;
} else {
$temp_w = ($height / $source_h) * $source_w;
}
break;
}
// Create temp resized image
$bgcolor_default = apply_filters( 'wpsc_preview_image_bgcolor', array( 255, 255, 255 ) );
$temp_img = ImageCreateTrueColor( $temp_w, $temp_h );
$bgcolor = ImageColorAllocate( $temp_img, $bgcolor_default[0], $bgcolor_default[1], $bgcolor_default[2] );
ImageFilledRectangle( $temp_img, 0, 0, $temp_w, $temp_h, $bgcolor );
ImageAlphaBlending( $temp_img, true );
ImageCopyResampled( $temp_img, $src_img, 0, 0, 0, 0, $temp_w, $temp_h, $source_w, $source_h );
$dst_img = ImageCreateTrueColor( $width, $height );
$bgcolor = ImageColorAllocate( $dst_img, $bgcolor_default[0], $bgcolor_default[1], $bgcolor_default[2] );
ImageFilledRectangle( $dst_img, 0, 0, $width, $height, $bgcolor );
ImageAlphaBlending( $dst_img, true );
// X & Y Offset to crop image properly
if ( $temp_w < $width ) {
$w1 = ($width / 2) - ($temp_w / 2);
} else if ( $temp_w == $width ) {
$w1 = 0;
} else {
$w1 = ($width / 2) - ($temp_w / 2);
}
if ( $temp_h < $height ) {
$h1 = ($height / 2) - ($temp_h / 2);
} else if ( $temp_h == $height ) {
$h1 = 0;
} else {
$h1 = ($height / 2) - ($temp_h / 2);
}
switch ( $scaling_method ) {
case 'cropping':
ImageCopy( $dst_img, $temp_img, $w1, $h1, 0, 0, $temp_w, $temp_h );
break;
case 'non-cropping':
default:
ImageCopy( $dst_img, $temp_img, 0, 0, 0, 0, $temp_w, $temp_h );
break;
}
$image_quality = wpsc_image_quality();
ImageAlphaBlending( $dst_img, false );
switch ( $imagetype[2] ) {
case IMAGETYPE_JPEG:
header( "Content-type: image/jpeg" );
imagejpeg( $dst_img );
imagejpeg( $dst_img, WPSC_CACHE_DIR . $cache_filename . '.jpg', $image_quality );
# chmod( WPSC_CACHE_DIR . $cache_filename . ".jpg", 0775 );
break;
case IMAGETYPE_GIF:
header( "Content-type: image/gif" );
ImagePNG( $dst_img );
ImagePNG( $dst_img, WPSC_CACHE_DIR . $cache_filename . ".gif" );
# chmod( WPSC_CACHE_DIR . $cache_filename . ".gif", 0775 );
break;
case IMAGETYPE_PNG:
header( "Content-type: image/png" );
ImagePNG( $dst_img );
ImagePNG( $dst_img, WPSC_CACHE_DIR . $cache_filename . ".png" );
# chmod( WPSC_CACHE_DIR . $cache_filename . ".png", 0775 );
break;
default:
$pass_imgtype = false;
break;
}
exit();
}
}
}
}
}
Any suggestions or help on how to resolve this would be greatly appreciated!
You errors come from getimagesize() here you can find the doc, it can return "false" witch is you case here, cause your image file is impossible to access, I suppose. https://www.php.net/manual/en/function.getimagesize.php
You should add verification on return value getimagesize. Or use try/catch
How to an add user_id ($_POST['pk']) at the beginning of the filename when I upload an image to server with the below function update_customer().
With this I can easily discover the correct files of a specific user.
Result should be:
filename = $_POST['pk'].'-'.$_POST['name']
function update_customer() {
if(isset($_POST['name']) && isset($_POST['pk'])) {
//print_r($_FILES['image']);exit;
$filename = '';
if(isset($_FILES['image']) && $_FILES['image']['tmp_name'] && $_FILES['image']['error'] == 0) {
$filename = $_FILES['image']['name'];
$upload_dir = $_SERVER['DOCUMENT_ROOT'].'/wp-content/uploads/agent_company_logos/';
$wp_filetype = wp_check_filetype_and_ext( $_FILES['image']['tmp_name'], $_FILES['image']['name'] );
if($wp_filetype['proper_filename'])
$_FILES['image']['name'] = $wp_filetype['proper_filename'];
if ( ( !$wp_filetype['type'] || !$wp_filetype['ext'] ) ) {
$arrErrors['file'] = __('File type not allowed', 'agent-plugin');
}
else {
move_uploaded_file($_FILES['image']['tmp_name'], $upload_dir.$_FILES['image']['name']);
}
if(!empty($arrErrors) && count($arrErrors) == 0) {
die (json_encode(array('file' => agent_get_user_file_path($_FILES['image']['name']), 'caption' => '', 'status' => 1)));
}
//echo $_FILES['image']['name'];
}
$updated = $GLOBALS['wpdb']->update($GLOBALS['wpdb']->prefix.'agent_customer', array('company_logo' => $filename), array('user_id' => $_POST['pk']));
$dir = wp_upload_dir();
echo $dir['baseurl'] . '/agent_company_logos/'.$filename; exit(0);
}
echo 0;
die();
}
you can use
$basname = $_POST['pk'].basename($file);
I'm new to php and I'm trying to figure out how to sort images by exif creation date. The code below is the one from this tutorial I followed: Simple Php Gallery Pagination
I just modified a little bit to retrieve exif data
I'm looking for a method without database, the goal is to have a paginated gallery (and now it is paginated) sorted with newest first
function getPictures() {
global $page, $per_page, $has_previous, $has_next, $DirFoto, $DirThumb;
if ( $handle = opendir($DirFoto) ) {
echo '<ul id="pictures">';
$count = 0;
$skip = $page * $per_page;
if ( $skip != 0 )
$has_previous = true;
while ( $count < $skip && ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) && ($type = getPictureType($file)) != '' )
$count++;
}
while ( $count < $per_page && ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
$exif = exif_read_data("$DirFoto/$file", 0, true);
if ( ! is_dir($DirThumb) ) {
mkdir($DirThumb);
}
if ( ! file_exists($DirThumb.'/'.$file) ) {
makeThumb( $file, $type );
}
echo '<li><a href="'.$DirFoto.'/'.$file.'" title="Photo taken on '.date("F d Y, H:i:s", strtotime($exif['IFD0']['DateTime'])).'">';
echo '<img src="'.$DirThumb.'/'.$file.'" alt="'.date("F d Y, H:i:s", strtotime($exif['IFD0']['DateTime'])).'"/>';
echo '</a></li>';
$count++;
}
}
echo '</ul>';
while ( ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
$has_next = true;
break;
}
}
}
}
I rationalized a bit the whole code, and this is my result:
$ScriptsHead = '
<link type="text/css" media="screen" rel="stylesheet" href="./stile.css"/>
<link type="text/css" rel="stylesheet" href="./js/photoswipe.css"/>
<script type="text/javascript" src="./js/jquery-1.8.3.js"></script>
<script type="text/javascript" src="./js/klass.min.js"></script>
<script type="text/javascript" src="./js/code.photoswipe-3.0.5.min.js"></script>
<script type="text/javascript" src="./js/miophotoswipe.js"></script>
';
function getPictures() {
global $page, $per_page, $has_previous, $has_next, $DirFoto, $DirThumb;
if ( $handle = opendir($DirFoto) ) {
$skip = $page * $per_page;
$images = array(); # empty data structure
while(($file = readdir($handle)) !== false ) {
if($file == '..' || $file == '.' || is_dir($file) || getPictureType($file) == '')
continue;
# only for images
$exif = exif_read_data("$DirFoto/$file", 0, true);
$date = $exif['IFD0']['DateTime']; # everything you like to be ordered
$images[$file] = $date; # associate each file to its date
}
asort($images); # sort the structure by date
echo '<ul id="pictures">';
if ( $skip != 0 )
$has_previous = true;
$count = -1;
foreach ($images as $file => $fileDate) {
$count ++;
if($count < $skip)
continue;
if($count >= $skip + $per_page) {
$has_next = true;
break;
}
if ( ! is_dir($DirThumb) ) {
mkdir($DirThumb);
}
if ( ! file_exists($DirThumb.'/'.$file) ) {
makeThumb( $file, $type );
}
echo '<li><a href="'.$DirFoto.'/'.$file.'" title="Photo taken on '.date("F d Y, H:i:s", strtotime($fileDate)).'">';
echo '<img src="'.$DirThumb.'/'.$file.'" alt="'.date("F d Y, H:i:s", strtotime($fileDate)).'"/>';
echo '</a></li>';
}
echo '</ul>';
}
}
function getPictureType($file) {
$split = explode('.', $file);
$ext = $split[count($split) - 1];
if ( preg_match('/jpg|jpeg/i', $ext) ) {
return 'jpg';
} else if ( preg_match('/png/i', $ext) ) {
return 'png';
} else if ( preg_match('/gif/i', $ext) ) {
return 'gif';
} else {
return '';
}
}
function makeThumb( $file, $type ) {
global $max_width, $max_height, $DirFoto, $DirThumb;
if ( $type == 'jpg' ) {
$src = imagecreatefromjpeg($DirFoto.'/'.$file);
} else if ( $type == 'png' ) {
$src = imagecreatefrompng($DirFoto.'/'.$file);
} else if ( $type == 'gif' ) {
$src = imagecreatefromgif($DirFoto.'/'.$file);
}
if ( ($oldW = imagesx($src)) < ($oldH = imagesy($src)) ) {
$newW = $oldW * ($max_width / $oldH);
$newH = $max_height;
} else {
$newW = $max_width;
$newH = $oldH * ($max_height / $oldW);
}
$new = imagecreatetruecolor($newW, $newH);
imagecopyresampled($new, $src, 0, 0, 0, 0, $newW, $newH, $oldW, $oldH);
if ( $type == 'jpg' ) {
imagejpeg($new, $DirThumb.'/'.$file);
} else if ( $type == 'png' ) {
imagepng($new, $DirThumb.'/'.$file);
} else if ( $type == 'gif' ) {
imagegif($new, $DirThumb.'/'.$file);
}
imagedestroy($new);
imagedestroy($src);
}
/* echo phpinfo(); */
?>
Since the code reads through the directory and outputs HTML as it goes, you will have to change it up a bit to do what you want. I suggest first reading the filenames into an array and call exif_read_data for each file read.
If you key the array by the filename and the value of the array is the exif creation date, you can then call asort() to sort the array by creation date. If a file doesn't have a valid exif creation date, perhaps you could just use the modification time of the file on the server.
Once the array is sorted in the proper order, you could change the following while loop
while ( $count < $per_page && ($file = readdir($handle)) !== false ) {
to be
while ( $count < $per_page && ($file = array_shift($sorted_files)) !== false ) {
where $sorted_files is the array of sorted files. Hope that helps, if not I can try to write up an example.
Sorry I can't try this solution right now, but it should be something like:
<?php
function getPictures() {
global $page, $per_page, $has_previous, $has_next, $DirFoto, $DirThumb;
if ( $handle = opendir($DirFoto) ) {
$images = array(); # empty data structure
while(($file = readdir($handle)) !== false ) {
if ( !is_dir($file) && ($type = getPictureType($file)) != '' )
# only for images
$exif = exif_read_data("$DirFoto/$file", 0, true);
$date = $exif['IFD0']['DateTime']; # everything you like to be ordered
$images[$file] = $date; # associate each file to its date
}
}
asort($images); # sort the structure by date
echo '<ul id="pictures">';
$skip = $page * $per_page;
if ( $skip != 0 )
$has_previous = true;
$count = 0;
while ( $count < $skip && ($file = array_shift($images)) !== false ) {
if ( !is_dir($file) && ($type = getPictureType($file)) != '' )
$count++;
}
# $count = 0;# (???)
while ( $count < $per_page && ($file = array_shift($images)) !== false ) {
$exif = exif_read_data("$DirFoto/$file", 0, true); # it could be avoided
if ( ! is_dir($DirThumb) ) {
mkdir($DirThumb);
}
if ( ! file_exists($DirThumb.'/'.$file) ) {
makeThumb( $file, $type );
}
echo '<li><a href="'.$DirFoto.'/'.$file.'" title="Photo taken on '.date("F d Y, H:i:s", strtotime($exif['IFD0']['DateTime'])).'">';
echo '<img src="'.$DirThumb.'/'.$file.'" alt="'.date("F d Y, H:i:s", strtotime($exif['IFD0']['DateTime'])).'"/>';
echo '</a></li>';
$count++;
}
echo '</ul>';
while ( ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
$has_next = true;
break;
}
}
}
}
?>
Please give me a feedback on that.
Cheers,
Riccardo
The code works now, if I mouseover the tooltip shows me the right date but no thumbnails or images are loaded.
In my first question I omitted the last two functions, this is my current update code:
<?php
$page = $_GET['page'];
$has_previous = false;
$has_next = false;
function getPictures() {
global $page, $per_page, $has_previous, $has_next, $DirFoto, $DirThumb;
if ( $handle = opendir($DirFoto) ) {
$images = array();
while(($file = readdir($handle)) !== false ) {
if ( !is_dir($file) && ($type = getPictureType($file)) != '' )
$exif = exif_read_data("$DirFoto/$file", 0, true);
$date = $exif['IFD0']['DateTime'];
$images[$file] = $date;
}
}
asort($images);
echo '<ul id="pictures">';
$skip = $page * $per_page;
if ( $skip != 0 )
$has_previous = true;
$count = 0;
while ( $count < $skip && ($file = array_shift($images)) !== false ) {
if ( !is_dir($file) && ($type = getPictureType($file)) != '' )
$count++;
}
while ( $count < $per_page && ($file = array_shift($images)) !== false ) {
$exif = exif_read_data("$DirFoto/$file", 0, true);
if ( ! is_dir($DirThumb) ) {
mkdir($DirThumb);
}
if ( ! file_exists($DirThumb.'/'.$file) ) {
makeThumb( $file, $type );
}
echo '<li><a href="'.$DirFoto.'/'.$file.'" title="Photo taken on '.date("F d Y, H:i:s", strtotime($exif['IFD0']['DateTime'])).'">';
echo '<img src="'.$DirThumb.'/'.$file.'" alt="'.date("F d Y, H:i:s", strtotime($exif['IFD0']['DateTime'])).'"/>';
echo '</a></li>';
$count++;
}
echo '</ul>';
while ( ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
$has_next = true;
break;
}
}
}
function getPictureType($file) {
$split = explode('.', $file);
$ext = $split[count($split) - 1];
if ( preg_match('/jpg|jpeg/i', $ext) ) {
return 'jpg';
} else if ( preg_match('/png/i', $ext) ) {
return 'png';
} else if ( preg_match('/gif/i', $ext) ) {
return 'gif';
} else {
return '';
}
}
function makeThumb( $file, $type ) {
global $max_width, $max_height;
if ( $type == 'jpg' ) {
$src = imagecreatefromjpeg($DirFoto.'/'.$file);
} else if ( $type == 'png' ) {
$src = imagecreatefrompng($DirFoto.'/'.$file);
} else if ( $type == 'gif' ) {
$src = imagecreatefromgif($DirFoto.'/'.$file);
}
if ( ($oldW = imagesx($src)) < ($oldH = imagesy($src)) ) {
$newW = $oldW * ($max_width / $oldH);
$newH = $max_height;
} else {
$newW = $max_width;
$newH = $oldH * ($max_height / $oldW);
}
$new = imagecreatetruecolor($newW, $newH);
imagecopyresampled($new, $src, 0, 0, 0, 0, $newW, $newH, $oldW, $oldH);
if ( $type == 'jpg' ) {
imagejpeg($new, $DirThumb.'/'.$file);
} else if ( $type == 'png' ) {
imagepng($new, $DirThumb.'/'.$file);
} else if ( $type == 'gif' ) {
imagegif($new, $DirThumb.'/'.$file);
}
imagedestroy($new);
imagedestroy($src);
}
?>
after a long search on google i found below php code for image gallery, but it produce jquery effect after cick on thumbnail, instead of jquery i want to go to other page that shows full respective image, like a wordpress blog does!
<?php
# SETTINGS
$max_width = 200;
$max_height = 200;
$per_page = 9;
//$imagedir = 'gallery/';
$page = $_GET['page'];
$has_previous = false;
$has_next = false;
function getPictures() {
global $page, $per_page, $has_previous, $has_next;
if ( $handle = opendir(".") ) {
$lightbox = rand();
echo '<ul id="pictures">';
$count = 0;
$skip = $page * $per_page;
if ( $skip != 0 )
$has_previous = true;
while ( $count < $skip && ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) && ($type = getPictureType($file)) != '' )
$count++;
}
$count = 0;
while ( $count < $per_page && ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
if ( ! is_dir('thumbs') ) {
mkdir('thumbs');
}
if ( ! file_exists('thumbs/'.$file) ) {
makeThumb( $file, $type );
}
echo '<li><a href="'.$file.'" rel="lightbox['.$lightbox.']">';
echo '<img src="thumbs/'.$file.'" alt="" />';
echo '<div class="fb">view</div></a></li>';
$count++;
}
}
echo '</ul>';
while ( ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
$has_next = true;
break;
}
}
}
}
function getPictureType($file) {
$split = explode('.', $file);
$ext = $split[count($split) - 1];
if ( preg_match('/jpg|jpeg/i', $ext) ) {
return 'jpg';
} else if ( preg_match('/png/i', $ext) ) {
return 'png';
} else if ( preg_match('/gif/i', $ext) ) {
return 'gif';
} else {
return '';
}
}
function makeThumb( $file, $type ) {
global $max_width, $max_height;
if ( $type == 'jpg' ) {
$src = imagecreatefromjpeg($file);
} else if ( $type == 'png' ) {
$src = imagecreatefrompng($file);
} else if ( $type == 'gif' ) {
$src = imagecreatefromgif($file);
}
if ( ($oldW = imagesx($src)) < ($oldH = imagesy($src)) ) {
$newW = 220;
$newH = $max_height;
} else {
$newW = $max_width;
$newH = 200;
}
$new = imagecreatetruecolor($newW, $newH);
imagecopyresampled($new, $src, 0, 0, 0, 0, $newW, $newH, $oldW, $oldH);
if ( $type == 'jpg' ) {
imagejpeg($new, 'thumbs/'.$file);
} else if ( $type == 'png' ) {
imagepng($new, 'thumbs/'.$file);
} else if ( $type == 'gif' ) {
imagegif($new, 'thumbs/'.$file);
}
imagedestroy($new);
imagedestroy($src);
}
?>
Remove the rel attribute from the anchor element in the while loop outputting the images. This would prevent the Lightbox script from binding events to this link, so when users click it, they will simply be redirected to the image specified in the href attribute.
In other words, instead of
echo '<li><a href="'.$file.'" rel="lightbox['.$lightbox.']">';
use
echo '<li><a href="'.$file.'">';
I have a PHP script that creates a thumbnail and lists an image gallery. The problem I'm having is that it lists it by timestamp on the server but I want it to list 'naturally'.
<?php
# SETTINGS
$max_width = 100;
$max_height = 100;
$per_page = 24;
$page = $_GET['page'];
$has_previous = false;
$has_next = false;
function getPictures() {
global $page, $per_page, $has_previous, $has_next;
if ( $handle = opendir(".") ) {
$lightbox = rand();
echo '<ul id="pictures">';
$count = 0;
$skip = $page * $per_page;
if ( $skip != 0 )
$has_previous = true;
while ( $count < $skip && ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) && ($type = getPictureType($file)) != '' )
$count++;
}
$count = 0;
while ( $count < $per_page && ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
if ( ! is_dir('thumbs') ) {
mkdir('thumbs');
}
if ( ! file_exists('thumbs/'.$file) ) {
makeThumb( $file, $type );
}
echo '<li><a href="'.$file.'" class="zoom" rel="group">';
echo '<img src="thumbs/'.$file.'" alt="" />';
echo '</a></li>';
$count++;
}
}
echo '</ul>';
while ( ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
$has_next = true;
break;
}
}
}
}
function getPictureType($file) {
$split = explode('.', $file);
$ext = $split[count($split) - 1];
if ( preg_match('/jpg|jpeg/i', $ext) ) {
return 'jpg';
} else if ( preg_match('/png/i', $ext) ) {
return 'png';
} else if ( preg_match('/gif/i', $ext) ) {
return 'gif';
} else {
return '';
}
}
function makeThumb( $file, $type ) {
global $max_width, $max_height;
if ( $type == 'jpg' ) {
$src = imagecreatefromjpeg($file);
} else if ( $type == 'png' ) {
$src = imagecreatefrompng($file);
} else if ( $type == 'gif' ) {
$src = imagecreatefromgif($file);
}
if ( ($oldW = imagesx($src)) < ($oldH = imagesy($src)) ) {
$newW = $oldW * ($max_width / $oldH);
$newH = $max_height;
} else {
$newW = $max_width;
$newH = $oldH * ($max_height / $oldW);
}
$new = imagecreatetruecolor($newW, $newH);
imagecopyresampled($new, $src, 0, 0, 0, 0, $newW, $newH, $oldW, $oldH);
if ( $type == 'jpg' ) {
imagejpeg($new, 'thumbs/'.$file);
} else if ( $type == 'png' ) {
imagepng($new, 'thumbs/'.$file);
} else if ( $type == 'gif' ) {
imagegif($new, 'thumbs/'.$file);
}
imagedestroy($new);
imagedestroy($src);
}
?>
similair to hobodave but i would use php's built in natsort function:
uksort($output, "strnatcmp");
You're not even using an array.
Instead of echo'ing your li's as you encounter them you need to put them into an array, indexed by filename.
$output[$file] = '<li>etc</li>';
Then once your loop has completed, you'll need to use a custom function to do a natural key sort, since PHP's natsort() only works on values.
function natksort($array) {
$keys = array_keys($array);
natsort($keys);
$ret = array();
foreach ($keys as $k) {
$ret[$k] = $array[$k];
}
return $ret;
}
$output = natksort($output);
echo '<ul>';
foreach ($output as $out) {
echo $out;
}
echo '</ul>';
Edit
Wow, I found this little gem to do the sorting:
uksort($array, 'strnatcasecmp');
Credit: http://www.chipstips.com/?p=269
The trick was to put everything inside an array... I took the liberty of rewriting your getPictures() function... This one implements the sorting.
function getPictures() {
global $page, $per_page, $has_previous, $has_next;
if (!is_dir('thumbs')) {
mkdir('thumbs');
}
if ($handle = opendir(".")) {
$lightbox = rand();
$files = array();
while (($file = readdir($handle)) !== false) {
if (!is_dir($file) && ($type = getPictureType($file)) != '') {
$files[] = $file;
}
}
natsort($files);
$has_previous = $skip != 0;
$has_next = (count($files) - $skip - $per_page) > 0;
$spliceLength = min($per_page, count($files) - $skip);
$files = array_slice($files, $skip, $spliceLength);
echo '<ul id="pictures">';
foreach($files as $file) {
if (!file_exists('thumbs/' . $file)) {
$type = getPictureType($file);
makeThumb($file, $type);
}
echo '<li><a href="' . $file . '" class="zoom" rel="group">';
echo '<img src="thumbs/' . $file . '" alt="" />';
echo '</a></li>';
}
echo '</ul>';
}
}