TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-15-2008, 04:00 PM   #1 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default cropping images ( this style ) [REQUEST]

how do I do this:
http://garrysmod.com/challenge/image...30&w=125&h=250

I made a crop image which centers itself from the corridinates by cropped images, and dividing by 2 but.. how do I achieve this effect, by the way, go ahead and increase the w and h url vars to see more.
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-16-2008, 02:35 AM   #2 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

[bump] 12346 vvvv
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-17-2008, 05:55 AM   #3 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

I did a quick thumbnail function here to show you how simple it really is, it might not be working as im pretty tired and I was about to go to bed ;)

PHP Code:
<?php
    
/** Assuming $source is an image resource to the image we need to alter */

    /** Max width + height */
    
$width 100;
    
$height 100;

    
$factor 1;

    if(
imagesx($source) > $width)
    {
        
$factor = ($width imagesx($source));
    }

    if((
imagesy($source) * $factor) > $height)
    {
        
$factor = ($height imagesy($source));
    }

    
$width floor(imagesx($source) * $factor);
    
$height floor(imagesy($source) * $factor);

    
$thumbnail imagecreatetruecolor($width$height);
    
$bg imagecolorallocate($thumbnail255255255);
    
imagefilledrectangle($thumbnail00, (imagesx($thumbnail) - 1), (imagesy($thumbnail) - 1), $bg);

    
imagecopyresampled($thumbnail$source0000$width$heightimagesx($source), imagesy($source));
    
imagedestroy($source);

    
$source NULL;

    
/** $thumbnail is now the generated thumbnail */
?>
__________________

Last edited by Kalle : 05-19-2008 at 12:49 AM.
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
The Following User Says Thank You to Kalle For This Useful Post:
Orc (05-18-2008)
Old 05-18-2008, 10:26 PM   #4 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Kalle View Post
I did a quick thumbnail function here to show you how simple it really is, it might not be working as im pretty tired and I was about to go to bed ;)

PHP Code:
<?php
    
/** Assuming $source is an image resource to the image we need to alter */

    /** Max width + height */
    
$width 100;
    
$height 100;

    
$factor 1;

    if(
imagesx($source) > $width)
    {
        
$factor = ($width imagesx($source));
    }

    if((
imagesy($source $factor) > $height)
    {
        
$factor = ($height imagesy($source));
    }

    
$width floor(imagesx($source) * $factor);
    
$height floor(imagesy($source) * $factor);

    
$thumbnail imagecreatefromtruecolor($width$height);
    
$bg imagecolorallocate($thumbnail255255255);
    
imagefilledrectangle($thumbnail00, (imagesx($thumbnail) - 1), (imagesy($thumbnail) - 1), $bg);

    
imagecopyresampled($thumbnail$source0000$width$heightimagesx($source), imagesy($source));
    
imagedestroy($source);

    
$source NULL;

    
/** $thumbnail is now the generated thumbnail */
?>
Didn't work at all :P
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-19-2008, 12:47 AM   #5 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

Hmm I fixed the code, but I see it wasn't what you wanted in first place, but thats what you get out of being up late >.<
__________________
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
Old 05-19-2008, 06:29 AM   #6 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Kalle View Post
Hmm I fixed the code, but I see it wasn't what you wanted in first place, but thats what you get out of being up late >.<
xD, it's alright. I'll figure it out some how.
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-19-2008, 08:35 AM   #7 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

PHP: imagecopyresized - Manual

Will do what you want, look specifically at SRC_X and SRC_Y -- you can tell it what coordinates you want to start the copy at, effectively creating a cropped area.
-m
delayedinsanity is offline  
Reply With Quote
Old 05-19-2008, 08:37 AM   #8 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by delayedinsanity View Post
PHP: imagecopyresized - Manual

Will do what you want, look specifically at SRC_X and SRC_Y -- you can tell it what coordinates you want to start the copy at, effectively creating a cropped area.
-m
I use resampled
:P
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-19-2008, 04:37 PM   #9 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Okay, but it can still do it, and in this case if all you want to do is create a cropped area you don't need to use resampled, because you're not resizing. You're just chopping some off.
-m

edit: I just looked closer at your example. It's not cropping like I originally thought, it's just resizing. So that should be easy? Let me try something here...
delayedinsanity is offline  
Reply With Quote
Old 05-19-2008, 04:59 PM   #10 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Okay, so theirs isn't just resizing, it's cropping too, based on what size you enter... so this is pretty close, all mine does is resize it, but I just woke up. I think they're probably using some kind of math to figure out a crop area based on the ratio of the size entered. I didn't tinker with that at all. Also i'm not sure if you can dynamically display the new image without saving it as an actual physical file somewhere. If you can, I'd love to know how, if anybody knows?

PHP Code:
<?php
function thumbnail ($szFile$intNewWidth$intNewHeight) {

    
$aImg getimagesize($szFile);
        
$intWidth $aImg[0];
        
$intHeight $aImg[1];
        
$szFileType $aImg['mime'];
    unset(
$aImg);

    switch (
$szFileType) {
        case 
"image/pjpeg": case "image/jpeg"$imgNew imagecreatefromjpeg($szFile); break;
        case 
"image/x-png": case "image/png":  $imgNew imagecreatefrompng($szFile); break;
        case 
"image/gif"$imgNew imagecreatefromgif($szFile); break;
        default: return 
false;
    }

    
$imgResized imagecreatetruecolor($intNewWidth$intNewHeight);
    
imagecopyresampled($imgResized$imgNew0000$intNewWidth$intNewHeight$intWidth$intHeight);
    if (
imagejpeg($imgResized"temp/temp.jpg"100)) {
        
imagedestroy($imgResized);
        
imagedestroy($imgNew);
        return 
true;
    } else {
        
imagedestroy($imgResized);
        
imagedestroy($imgNew);
        return 
false;
    }

// thumbnail()

$file "path/to/your.jpg";
thumbnail($file$_GET['w'], $_GET['h']);
?>

<img src="temp/temp.jpg" />
-m
delayedinsanity is offline  
Reply With Quote
Old 05-19-2008, 09:38 PM   #11 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by delayedinsanity View Post
Okay, so theirs isn't just resizing, it's cropping too, based on what size you enter... so this is pretty close, all mine does is resize it, but I just woke up. I think they're probably using some kind of math to figure out a crop area based on the ratio of the size entered. I didn't tinker with that at all. Also i'm not sure if you can dynamically display the new image without saving it as an actual physical file somewhere. If you can, I'd love to know how, if anybody knows?

PHP Code:
<?php
function thumbnail ($szFile$intNewWidth$intNewHeight) {

    
$aImg getimagesize($szFile);
        
$intWidth $aImg[0];
        
$intHeight $aImg[1];
        
$szFileType $aImg['mime'];
    unset(
$aImg);

    switch (
$szFileType) {
        case 
"image/pjpeg": case "image/jpeg"$imgNew imagecreatefromjpeg($szFile); break;
        case 
"image/x-png": case "image/png":  $imgNew imagecreatefrompng($szFile); break;
        case 
"image/gif"$imgNew imagecreatefromgif($szFile); break;
        default: return 
false;
    }

    
$imgResized imagecreatetruecolor($intNewWidth$intNewHeight);
    
imagecopyresampled($imgResized$imgNew0000$intNewWidth$intNewHeight$intWidth$intHeight);
    if (
imagejpeg($imgResized"temp/temp.jpg"100)) {
        
imagedestroy($imgResized);
        
imagedestroy($imgNew);
        return 
true;
    } else {
        
imagedestroy($imgResized);
        
imagedestroy($imgNew);
        return 
false;
    }

// thumbnail()

$file "path/to/your.jpg";
thumbnail($file$_GET['w'], $_GET['h']);
?>

<img src="temp/temp.jpg" />
-m
I have no idea, I know the person who made the site though, all he told me was he kept the aspect ratio the same, though I did that, and it still looks like shit. :P his is better. and he said to do it, was just math. -_- ugh.
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-19-2008, 10:29 PM   #12 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

If you know the person, tell him to cough up the code so you can dissect it and learn!
-m
delayedinsanity is offline  
Reply With Quote
Old 05-19-2008, 10:31 PM   #13 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by delayedinsanity View Post
If you know the person, tell him to cough up the code so you can dissect it and learn!
-m
Well alright, but hes not one to really give up his code. ;P

He has his own forums:
Facepunch Studios (Forums)

though I warn you, theres idiots about xD
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 05-21-2008, 03:33 PM   #14 (permalink)
The Contributor
 
j4v1's Avatar
 
Join Date: May 2008
Posts: 30
Thanks: 5
j4v1 is on a distinguished road
Default

[quote=delayedinsanity;14731]PHP: imagecopyresized - Manual

Will do what you want, look specifically at SRC_X and SRC_Y -- you can tell it what coordinates you want to start the copy at, effectively creating a cropped area.
-m[/QUoute]
************************************************** ********
One quick question. I'm trying to do the same thing. I'd like to start my cropping from a particular area/coordinates.

Below is the sample code. Also, if you follow the link to the original gif and then in your favorite editor crop the image so that its at the following dimension 438x519 from the center. I took the following pixels (left 0, Right 119, Top 476, Bottom 204). This above result is what I'm trying to get the following code to produce.

Any assistance again you may provide would truly be appreciated.

Thank you,
j4v1

PHP Code:
<?php
cropImage
(500460'http://images.wsdot.wa.gov/nwflow/flowmaps/sysvert.gif''gif''test500x460.gif');
function 
cropImage($nw$nh$source$stype$dest) {
$size getimagesize($source);
$w $size[0];
$h $size[1];
switch(
$stype) {
case 
'gif':
$simg imagecreatefromgif($source);
break;
case 
'jpg':
$simg imagecreatefromjpeg($source);
break;
case 
'png':
$simg imagecreatefrompng($source);
break;
}
$dimg imagecreatetruecolor($nw$nh);
$wm $w/$nw;
$hm $h/$nh;
$h_height $nh/2;
$w_height $nw/2;
if(
$w $h) {
$adjusted_width $w $hm;
$half_width $adjusted_width 2;
$int_width $half_width $w_height;
imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
} elseif((
$w $h) || ($w == $h)) {
$adjusted_height $h $wm;
$half_height $adjusted_height 2;
$int_height $half_height $h_height;
imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
} else {
imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
}
imagejpeg($dimg,$dest,100);
}
?>
j4v1 is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:10 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design