PDA

View Full Version : zooming images on a server


windec
02-17-2009, 02:56 PM
Iam a beginner and so please pardon the naiveness in my approach.
The iamge paths of 3 different resolutions are in a database and images-giver.php gives an xml. The idea is to load and display images of certain resolution based on zoomslider's value. callHTTPService is called only when the images are not already there.How to set the image source inside that function?. Better ideas are more than welcome.
inside <mx:Script>
private function zoomer(event:Event):void{
canvas.scaleX=zoomslider.value;
canvas.scaleY=zoomslider.value;

if(zoomslider.value<4){
var resolution=low;
}
if(zoomslider.value>8){
var resolution=high;
}
else{
var resolution=medium;
}

if(var imagesLoaded!=true){
callHTTPService(event:String);

}
}
public function callHTTPService(event:String){
getImages.send();
var imagesLoaded=true;
image1.source=;
}



<mx:HTTPService id="getImages" url="http://localhost/app/images-giver.php" method="POST" contentType="application/x-www-form-urlencoded" resultFormat="text" >
<mx:request>
<res>var resolution<res>
<name>picture1</name>
</mx:request>
</mx:HTTPService>


<mx:HSlider x="100" y="500" minimum="1" maximum="20" snapInterval=".1"
enabled="true" id="zoomslider" liveDragging="false" change="{zoomer(event)}"
allowTrackClick="false" toolTip="{zoomslider.value}"/>
inside the canvas there are images image1,image2 etc:

<mx:Image id="image1">
please note that I have posted only bits of code so that you can read it quickly.

wvxvw
02-17-2009, 03:35 PM
If you're already using PHP, why not use GD library? meaning, just send to PHP the dimentions of the image, and it'll send you that image back scaled. I'd use AMFPHP / ZendAMF for it + GD lib + RemoteObject.
I see if I can write an example when I'm home :)

wvxvw
02-17-2009, 06:54 PM
And... here goes the example :)
<?xml version="1.0" encoding="utf-8"?>
<!-- PictureLoaderAMF -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import flash.display.Loader;
import flash.events.Event;
import flash.utils.ByteArray;
import flash.utils.Endian;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.mxml.Operation;

private var _imageBytes:ByteArray;
private var _imageLoader:Loader;

public function clickHandler():void
{
service.resizedImage.send("images/test.jpg", int(widthText.text),
int(heightText.text));
}

public function resultHandler(event:ResultEvent):void
{
_imageBytes = event.result as ByteArray;
_imageBytes.position = 0;
_imageLoader = new Loader();
_imageLoader.contentLoaderInfo.addEventListener(Ev ent.COMPLETE,
imageLoader_completeHandler);
_imageLoader.loadBytes(_imageBytes);
}

private function imageLoader_completeHandler(event:Event):void
{
_imageLoader.contentLoaderInfo.removeEventListener (Event.COMPLETE,
imageLoader_completeHandler);
targetImage.width = _imageLoader.width;
targetImage.height = _imageLoader.height;
targetImage.data = _imageLoader.content;
}

public function faultHandler(event:FaultEvent):void
{
trace(event.fault);
}
]]>
</mx:Script>

<mx:RemoteObject id="service"
endpoint="http://localhost/amfphp/gateway.php"
destination="ImageService.resizedImage"
source="ImageService"
showBusyCursor="true">
<mx:method name="resizedImage"
result="resultHandler(event)"
fault="faultHandler(event)" />
</mx:RemoteObject>
<mx:Panel
title="PHP Image Resizer Example"
horizontalAlign="left"
width="100%"
height="100%"
paddingTop="10"
paddingLeft="10">
<mx:HBox>
<mx:VBox width="{width / 2}">
<mx:Image id="targetImage"/>
</mx:VBox>
<mx:VBox>
<mx:Label text="Desired width"/>
<mx:TextInput id="widthText"/>
<mx:Label text="Desired width"/>
<mx:TextInput id="heightText"/>
<mx:Button label="Reload Image"
click="clickHandler()"/>
</mx:VBox>
</mx:HBox>
</mx:Panel>
</mx:Application>
<?php
class ImageService
{
public function __construct(){ }

public function resizedImage($imagePath, $imageWidth, $imageHeight)
{
list($width, $height) = getimagesize($imagePath);
if (!$width || !$height) return null;
$newImage = imagecreatetruecolor($imageWidth, $imageHeight);
$image = imagecreatefromjpeg($imagePath);
imagecopyresampled($newImage, $image, 0, 0, 0, 0, $imageWidth, $imageHeight, $width, $height);
ob_start();
imagejpeg($newImage, null, 85);
$contents = ob_get_contents();
ob_end_clean();
imagedestroy($newImage);
return new ByteArray($contents);
}
}
?>
Ah, and this example requires you to have some JPEG file in your services directory in the "images/test.jpg".
I.e. if ImageService.php is in "services", then the image is in "services/images/test.jpg"