Solved

Transform the photo into pieces (squares or rectangles)

Posted on 2010-09-09
8
672 Views
Last Modified: 2013-11-08
Hi E's, I try to found the best solution, not matter if it is in imagemagick or in php, for get equal pieces of a picture like you can see in attach image.
In practice, if I have a picture width 800 * length 600, and if I want 12 pieces 4*3 (view attach image), I will get 12 pieces of 200*200 each one:
I will use get_image_size to know the size of the picture.
(variables width and length come from the get_image_size array)
$width = $width / 4; //determinate the width, it is 200px
$length = $length / 3 //determinate the length, it is 200px
Now it is the difficult part, it is give me 12 pieces 200*200 each and save in one folder of my server.

How I get the pieces of picture?

Regards, JC
pieces.png
0
Comment
Question by:Pedro Chagas
  • 4
  • 3
8 Comments
 
LVL 14

Expert Comment

by:existenz2
ID: 33644270
0
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 425 total points
ID: 33647572
Try this: with image you find as attachement it works fine.
<?php
error_reporting(E_ALL);
$src = imagecreatefromjpeg('Your_image_name_here.jpg');
$dest = imagecreatetruecolor(200,200);
$w=200;
$h=200;
$c = 0;
for ($y = 0; $y < 3; $y++) {
    for ($x = 0; $x < 4; $x++) {
        $c++;
        imagecopy($dest, $src, 0, 0, $w*$x, $h*$y, $w, $h);
        imagejpeg($dest, $c.'.jpg', 100);
    }
}
imagedestroy($dest);
imagedestroy($src);
?>

Open in new window

image12.jpg
0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 33648760
Hi @marqusG, I try your code and don't work fine.
The system create the image 1 to 12, but when I open the pieces, the pieces contain nothing! The size of each piece is about 1kb.
What is the problem?

Regards, JC
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 31

Expert Comment

by:Marco Gasi
ID: 33648983
Hi, joaochagas. Have you tried my snippet as is using image attached? In my system (Windows 7) all 12 files are created, they have size of 2.68 Kb and contains the expected portion of image.I post the screenshot of my thumbnails viewed in Explorer.
If you have tried my code with your image, maybe something goes wrong with your image... How you get image?
Maybe you can post the code wich grabs your image to see if there is something wrong in it.

2010-09-10-201450.jpg
0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 33651167
Hi, I post the code and attach the image I use to do the thumbs.

Regards, JC
error_reporting(E_ALL);
$src = imagecreatefromjpeg('for_puzzle.jpg');
$dest = imagecreatetruecolor(200,200);
$w=460;
$h=345;
$c = 0;
for ($y = 0; $y < 3; $y++) {
    for ($x = 0; $x < 4; $x++) {
        $c++;
        imagecopy($dest, $src, 0, 0, $w*$x, $h*$y, $w, $h);
        imagejpeg($dest, $c.'.jpg', 100);
    }
}
imagedestroy($dest);
imagedestroy($src);
?>

Open in new window

for-puzzle.jpg
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 33652250
In your question you said to " have a picture width 800 * length 600" but now the picture has a different dimension, so there are three cases:
1 - you resize image to obtain a picture 800x600;
2 - you change accordingly the piece's size : 460/4=115, 345/3=115
3 - you decrease the number of pieces (if picture size allows it: 460/200=2.3 so in this case you can't choose this third alternative).

Besides, in your script you set $w and $h to the picture's sizes but this is wrong. If you look at my original code, $w and $h was set to piece's size, that is 200.

At the end, if you want to use for-puzzle.jpg use attached code.

Best
<?php
error_reporting(E_ALL);
$src = imagecreatefromjpeg('for-puzzle.jpg');
$dest = imagecreatetruecolor(115,115);
$w=115;
$h=115;
$c = 0;
for ($y = 0; $y < 3; $y++) {
    for ($x = 0; $x < 4; $x++) {
        $c++;
//        echo $w*$x.",  " .$h*$x. "<br>";
        imagecopy($dest, $src, 0, 0, $w*$x, $h*$y, $w, $h);
        imagejpeg($dest, $c.'.jpg', 100);
    }
}
imagedestroy($dest);
imagedestroy($src);
?>

Open in new window

0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 33653809
Hi, with 800*600 picture work well.
Why don't work with my picture 460*345?
I change $w and $h to 115!!!
I don't have always pictures with 800*600, I can have other formats!

Regards, JC
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 33653821
because, I'm sure, you forgot to change this line

$dest = imagecreatetruecolor(200,200);

this way

$dest = imagecreatetruecolor(115,115);


0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question