Page 1 of 1

Warning: Invalid argument

Posted: Wed Nov 14, 2012 5:12 pm
by Fidbeck
Hi there

I followed Jacek's tut to create a image gallery but i'm getting a error

[syntax=php]<?php

//Script to create thumbnails
if (isset($_GET['img'])) {
//make thumbnail
if (file_exists($_GET['img'])) {
//Ignore user action if user click stop
ignore_user_abort(TRUE);
//set time to create thumbnails
set_time_limit(120);
//modifies the memory limit to create the thumbnails
ini_set('memory_limit', '512M');

$src_size = getimagesize($_GET['img']);

if ($src_size === FALSE) {
//ATTENTION - for the purpose of this tut its going to be used die but in a Login script that doesn't work because that will die the script and nothing else will be run
die('That does not look like a image.');
}

$thumb_width = 250;
$thumb_height = 200;

if ($src_size['mime'] === 'image/jpeg') {
$src = imagecreatefromjpeg($_GET['img']);
} else if ($src_size['mime'] === 'image/png') {
$src = imagecreatefrompng($_GET['img']);
} else if ($src_size['mime'] === 'image/gif') {
$src = imagecreatefromgif($_GET['img']);
} //else {
//header(Location: );

$src_aspect = round(($src_size[0] / $src_size[1]), 1);
$thumb_aspect = round(($thumb_width / $thumb_height), 1);

if ($src_aspect < $thumb_aspect) {
//higher
//Get the amunt to be reduced, decimal number.
$new_size = array(
$thumb_width,
($thumb_width / $src_size[0]) * $src_size[1]
);
$src_pos = array(
0,
(($new_size[1] - $thumb_height) * ($src_size[1] / $new_size[1])) / 2
);

} else if ($src_aspect > $thumb_aspect) {
//wider
$new_size = array(
($thumb_width / $src_size[1]) * $src_size[0],
$thumb_height
);
$src_pos = array(
(($new_size[0] - $thumb_width) * ($src_size[0] / $new_size[0])) / 2,
0
);

} else {
//same shape
$new_size = array(
$thumb_width,
$thumb_height
);
$src_pos(0, 0);
}

if ($new_size[0] < 1) {
$new_size[0] = 1;
}
if ($new_size[1] < 1) {
$new_size[1] = 1;
}

$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
/*
dst_image - destination
src_image - where you copying from
dst_x - destination position
dst_y - destination position
src_x - where you want to start copying from
src_y - new size
dst_w - new size
dst_h
src_w
src_h
*/
imagecopyresampled($thumb, $src, 0, 0, $src_pos[0], $src_pos[1], $new_size[0], $new_size[1], $src_size[0], $src_size[1]);

if ($src_size['mime'] === 'image/jpeg') {
imagejpeg($thumb, "thumbs/{$_GET['img']}");
} else if ($src_size['mime'] === 'image/png') {
imagepng($thumb, "thumbs/{$_GET['img']}");
} else if ($src_size['mime'] === 'image/gif') {
imagegif($thumb, "thumbs/{$_GET['img']}");
}

header("Location: thumbs/{$_GET['img']}");
}
die();
}

//Create the folder thumbs to store the thumbnails
if (is_dir('./image_gallery/thumbs') === FALSE) {
mkdir(('./image_gallery/thumbs'), 0744);
}

$images = glob('*.{jpg,jpeg,png,gif}', GLOB_BRACE);

?>

<script type="text/javascript"></script>
<!DOCTYPE html>

<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
a, img{
float: left;
padding: 5px;
}
</style>
</head>
<body>
<?php
foreach($images as $image) {
if (file_exists("./image_gallery/thumbs/{$image}")) {
echo "<a href='{$image}'><img src='thumbs/{$image}' alt='{$image}'/></a>";
} else {
echo "<a href='{$image}'><img src='?img=/{$image}' alt='{$image}'/></a>";
}
}
?>
</body>
</html>[/syntax]

Warning: Invalid argument supplied for foreach() in /home/a5510829/public_html/tryout/gallery.php on line 128

Re: Warning: Invalid argument

Posted: Wed Nov 14, 2012 7:23 pm
by Temor
It's telling you that $images is not a valid array. Try printing it out to see what happens.

[syntax=php]print_r($images);[/syntax]
if that doesn't work, try var dump
[syntax=php]var_dump($images);[/syntax]

Re: Warning: Invalid argument

Posted: Wed Nov 14, 2012 9:53 pm
by Fidbeck
retyped the code and all seems fine now

http://exp3.comyr.com/txaram/gallery.php

I changed the code but i'm getting an error, changed all $_GET['img'] to $img like this
$img = $_GET['img'];


but undifined index error :S

I silenced it but there is a way to fix without silencing...

Re: Warning: Invalid argument

Posted: Wed Nov 14, 2012 10:09 pm
by Fidbeck
instead of

[syntax=php]
$img = $_GET['img'];
if (isset($_GET['img'])) {
#rest of the code[/syntax]

I Tried

[syntax=php]if($img = isset($_GET['img'])){[/syntax]
and it fixed, no error and no problems

Re: Warning: Invalid argument

Posted: Sat Nov 17, 2012 12:31 am
by jacek
Fidbeck wrote:instead of

[syntax=php]
$img = $_GET['img'];
if (isset($_GET['img'])) {
#rest of the code[/syntax]

I Tried

[syntax=php]if($img = isset($_GET['img'])){[/syntax]
and it fixed, no error and no problems

Reassigning a variable does nothing but waste a bit of CPU time, also isset() returns wither true or false so setting that to $img will probably break the rest of the code. See my explanation in the other topic of the undefined index error.