Newb advice

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Re: Newb advice

Post by MiniMonty »

jackpf wrote:The only thing I've ever made in flash is an animation of someone shooting them self in the head :)
Hmmm...
is it as much fun as this toy I made ten years ago?
http://www.shutterbugclub.com/tax.html
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Newb advice

Post by jackpf »

Lol that's awesome.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Newb advice

Post by califdon »

I've never tried to do anything with the "real" Flash tools, but I did this with Xara Extreme: http://ravey.net/test/rollingdon.html
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Re: Newb advice

Post by MiniMonty »

Hi all,

so everything was going swimmingly and after a little persuasion my Flash front end started taking orders
from my php and loading up images for display in the gallery. But... some images are too big for the desired display space.
I started looking around the php tutorials for "resize" ideas and found a few but, again, I can't get any to work.
So before I post up any (badly indented) code, what are the main principles to get my head around and the obvious
pitfalls to avoid when I try to add "resize" to my lovely script ?

I have a tightly defined display area of 847x543 pixels and although images are welcome to be smaller they cannot be larger.
When the user uploads an image it's aspect ration must be maintained when resizing - I think that's about it but please
feel free to advise on any related areas I should look into.

Best wishes
Monty
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Newb advice

Post by jackpf »

You'll probably want to look into PHP's GD library.

There are loads of tutorials on resizing images on google.
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Re: Newb advice

Post by MiniMonty »

This is the info from my server about GD.
Is this an OK set up because as you say there are lots of tutorials etc around and they mostly seem to
rely upon having the GD library installed. Am I good to go with this set up ?

Image
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Newb advice

Post by jackpf »

Yes
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Re: Newb advice

Post by MiniMonty »

jackpf wrote:Yes
Groovy... Away we go !

So I'm looking at this function as a promising solution (see code below) but in line 2 it asks for a file path and no matter
what I put there I get a Syntax error returned from the server. I know the path and I'm using it in the script you have
all helped me create over the past day or so (so I know it's right). I've even tested it using the path as "11.jpg" with the php file
in the same directory as the test image.
Am I making a big fat newb mistake on paths ? Does it need special syntax to define it after the $ ?
I'm working on the basis that it simply should read: (on the server)

Code: Select all

 
function get_image_sizes($"/a_website/a_dir/images/uploads/11.jpg", 
  $maxResizeWidth, $maxResizeHeight) { 
 
Of course, later in this adventure I'll have look into running this function on the image that the user is
uploading and the function will have to be incorporated into the one grand script that we're creating - but for
now I just want to test and prove the resize thing to understand it.

I had thought I was starting to understand php as an OO language but getting a burp on line 2 has brought me right back
to wet behind the ears newb innocence.

All and any help much appreciated
Best wishes
Monty

Code: Select all

 
function get_image_sizes($sourceImageFilePath, 
  $maxResizeWidth, $maxResizeHeight) {
 
  // Get the width and height of the original image
  $size = getimagesize($sourceImageFilePath);
  if($size === FALSE) return FALSE; // Error condition
  $origWidth = $size[0];
  $origHeight = $size[1];
 
  // Change dimensions to fit maximum width and height
  $resizedWidth = $origWidth;
  $resizedHeight = $origHeight;
  if($resizedWidth > $maxResizeWidth) {
    $aspectRatio = $maxResizeWidth / $resizedWidth;
    $resizedWidth = round($aspectRatio * $resizedWidth);
    $resizedHeight = round($aspectRatio * $resizedHeight);
  }
  if($resizedHeight > $maxResizeHeight) {
    $aspectRatio = $maxResizeHeight / $resizedHeight;
    $resizedWidth = round($aspectRatio * $resizedWidth);
    $resizedHeight = round($aspectRatio * $resizedHeight);
  }
  
  // Return an array with the original and resized dimensions
  return array($origWidth, $origHeight, $resizedWidth, 
    $resizedHeight);
}
 
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Newb advice

Post by califdon »

I don't know anything about image resizing, but that dollar sign just before the quoted string must be removed. The dollar sign is simply the first character of all variable names, so when you replace a variable name with a literal string, the dollar sign must be removed as part of the variable name.
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Re: Newb advice

Post by MiniMonty »

Hmmmm...

still no joy with the resize.
I found this:
http://dt.in.th/2008-05-13.gd-php-resize-image.html

which looked very good but I still can't integrate it into my upload script. (quoted below)
Need some guru help here...

Best wishes
Monty

Code: Select all

 
<?php
define ("MAX_SIZE","100");
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$errors=0;
if(isset($_POST['Submit']))
{
$image=$_FILES['image']['name'];
if ($image)
{
$filename = stripslashes($_FILES['image']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
echo '<h1>Only try to upload .jpg, .jpeg, .png and .gif files!</h1>';
$errors=1;
}
else
{
$size=filesize($_FILES['image']['tmp_name']);
if ($size > MAX_SIZE*1024)
{
echo '<h1>You have exceeded the size limit!</h1>';
$errors=1;
}
$groovy = sizeof(glob("/images/uploads/*"));
$groovy = ++$groovy;
print $groovy;
$image_name=$groovy.'.'.$extension;
$newname="uploads/".$image_name;
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied)
{
echo '<h1>Copy unsuccessfull!</h1>';
$errors=1;
}}}}
if(isset($_POST['Submit']) && !$errors)
{
echo "<h1>File Uploaded Successfully!</h1>";
}
// Now make an array of the contents of the directory "uploads"
$array = glob('uploads/*');
// write the .txt file with the new array
$myFile = "gallarray.txt";
$fh = fopen($myFile, 'w+') or die("can't open file");
$stringData = "arse=images/".implode(",images/",$array);
fwrite($fh, $stringData);
fclose($fh);
// APPEND the .txt fil with the total image number
$myFile = "gallarray.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
$stringData = "&totalimgs=".$groovy;
fwrite($fh, $stringData);
fclose($fh);
?>
 
Last edited by MiniMonty on Mon Sep 07, 2009 10:23 am, edited 1 time in total.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Newb advice

Post by jackpf »

What's wrong with it?

That code looks as if it will copy a picture, resize it, and place it wherever you specify (although I haven't tested it). That sounds like exactly what you need.

What problems are you having?
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Re: Newb advice

Post by MiniMonty »

You're right - it does kind of work. Problem is that my originbal upload script is uploading the image to "uploads", renaming it numerically etc AND the included "doresize.php" is copying a resized version to the directory above and naming it (as it's been told to) as "sorted.jpg.

I need to combine the two so that I get the functionality of "doresize.php" tpo resize the image but only one copy saved into "uploads" which is a sub-directpory of "images" - and then renamed with the number convention etc.

I've tried a few things but I just get really big errors and I just can't unpick one script from the other !
I have a strong feeling it's about where I have put the call to "doresize.php" but I just don't know enough to know where it should go and what (if anything) it should replace.

code below:
1) big2.php (my original upload script with the "include" and at the end a call to "doresize.php"
2) doresize.php

Any help on this would be GREAT because this is 99% done and I really want to finish it.


My original upload script.

Code: Select all

 
<?php
include '/uploads/doresize.php';
define ("MAX_SIZE","100");
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$errors=0;
if(isset($_POST['Submit']))
{
$image=$_FILES['image']['name'];
if ($image)
{
$filename = stripslashes($_FILES['image']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
echo '<h1>Only try to upload .jpg, .jpeg, .png and .gif files!</h1>';
$errors=1;
}
else
{
$size=filesize($_FILES['image']['tmp_name']);
if ($size > MAX_SIZE*1024)
{
echo '<h1>You have exceeded the size limit!</h1>';
$errors=1;
}
$groovy = sizeof(glob("/Sites/com/shutterbugclub/www/images/uploads/*"));
$groovy = ++$groovy;
print $groovy;
$image_name=$groovy.'.'.$extension;
$newname="uploads/".$image_name;
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied)
{
echo '<h1>Copy unsuccessfull!</h1>';
$errors=1;
}}}}
if(isset($_POST['Submit']) && !$errors)
{
echo "<h1>File Uploaded Successfully!</h1>";
}
// make the call to doresize.php
img_resize ($_FILES [ 'image'] [ 'name'], $_FILES [ 'image'] [ 'tmp_name'], 150, 'sorted.jpg');
// Now make an array of the contents of the directory "uploads"
$array = glob('uploads/*');
// write the .txt file with the new array
$myFile = "gallarray.txt";
$fh = fopen($myFile, 'w+') or die("can't open file");
$stringData = "arse=images/".implode(",images/",$array);
fwrite($fh, $stringData);
fclose($fh);
// APPEND the .txt fil with the total image number
$myFile = "gallarray.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
$stringData = "&totalimgs=".$groovy;
fwrite($fh, $stringData);
fclose($fh);
?>
 
The resize script "doresize.php" borrowed from http://dt.in.th/2008-05-13.gd-php-resize-image.html whose instructions I've followed.

Code: Select all

 
<?php
 
// Image resize script by the DtTvB
 
function img_resize($photo_name, $from, $max, $to) {
    $ext = strtolower(end(explode('.',$photo_name)));
    if ($ext == 'jpg' || $ext == 'jpeg') {
        $im = imagecreatefromjpeg($from);
    } else if ($ext == 'gif') {
        $im = imagecreatefromgif($from);
    } else if ($ext == 'png') {
        $im = imagecreatefrompng($from);
    } else {
        return false;
    }
    $sx = imagesx($im);
    $sy = imagesy($im);
    $nx = $sx;
    $ny = $sy;
    if ($nx > $max) {
        $nx = $max;
        $ny = max(1, round($ny * ($nx / $sx)));
    }
    if ($ny > $max) {
        $ny = $max;
        $nx = max(1, round($nx * ($ny / $sy)));
    }
    $nm = imagecreatetruecolor($nx, $ny);
    imagealphablending ($nm, false);
    imagecopyresampled ($nm, $im, 0, 0, 0, 0, $nx, $ny, $sx, $sy);
    if ($ext == 'jpg' || $ext == 'jpeg') {
        imagejpeg ($nm, $to);
    } else if ($ext == 'gif') {
        imagegif ($nm, $to);
    } else if ($ext == 'png') {
        imagesavealpha ($nm, true);
        imagepng ($nm, $to);
    } else {
        return false;
    }
}
 
?>
 
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Newb advice

Post by jackpf »

Can't you just move the file? Or unlink() sorted.jpg once you've copied it?
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Re: Newb advice

Post by MiniMonty »

Really getting there now...

I moved the call to the "doresize" function and carefully rewrote it to include $newname - it now works a treat.
My final remaining "bug" is with creating the array in the .txt file.

Code: Select all

 
$myFile = "gallarray.txt";
$fh = fopen($myFile, 'w+') or die("can't open file");
$stringData = "arse=images/".implode(",images/",$array);
fwrite($fh, $stringData);
fclose($fh);
 
This outputs an array of the contents of the directory but NOT in numerical order (not in a human way anyway).
It writes it as any computer might:
images/uploads/1.jpg,images/uploads/10.jpg,images/uploads/11.jpg,images/uploads/12.jpg,images/uploads/13.jpg,images/uploads/14.jpg,images/uploads/15.jpg,images/uploads/16.jpg,images/uploads/17.jpg,images/uploads/18.jpg,images/uploads/19.jpg,images/uploads/2.jpg,images/uploads/20.jpg,

So image 2 is the 19th to display - no good !
Is there a way of reordering the data ?

Best wishes
Monty
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Newb advice

Post by jackpf »

Check this out :)

Code: Select all

$array = array('some/dir/1.jpg', 'some/dir/2.jpg', 'some/dir/6.jpg', 'some/dir/4.jpg', 'some/dir/3.jpg', 'some/dir/5.jpg');
 
function sort_files($file1, $file2)
{
    return (reset(explode('.', end(explode('/', $file1)))) > reset(explode('.', end(explode('/', $file2))))) ? 1 : -1;
}
 
uasort(&$array, 'sort_files');
 
header('Content-Type: text/plain');
print_r($array);
Post Reply